Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor C API #273

Open
wants to merge 57 commits into
base: main
Choose a base branch
from
Open

Refactor C API #273

wants to merge 57 commits into from

Conversation

edubart
Copy link
Contributor

@edubart edubart commented Sep 8, 2024

This is the actual implementation of the new C API overhaul. This is mostly finished and ready for review.

Added:

  • Added command line options for serializing machine config as JSON (--load-json-config and --store-json-config).
  • Lua API: Added new tojson/fromjson/tobase64/frombase64 functions.
  • Added support for VirtIO devices in JSON configs.
  • Added new method for receiving CMIO requests.

Changed:

  • The headers *-defines.h are not installed anymore (they are now considered implementation details which external people should not care about, try to depend on or bind).
  • Changed compiler for flags sanitize=yes and debug=yes to improve backtrace and catch more bugs.
  • Help and configs are now printed to stdout instead of stderr.
  • Simplified how Lua configs are serialized in cartesi-machine.lua, impacting its ordering.
  • All processor CSRs, x, f and uarch registers were grouped together into a single register enumeration.
  • Some Lua API functions have changed to reflect the new C API.
  • The CSRs htif.tohost/htif.fromhost promoted reason to be an official 2-bytes field, its data field shrunk to 4 bytes.
  • Changed sibling hashes ordering in JSON logs to match the expected ordering in the Lua test suite.
  • Changed jsonrpc API to reflect the new API changes, bumping its version to 0.0.2.
  • Bootargs changed to auto detect root flash drive filesystem (it will mount ext2 using ext4 driver).
  • Include default bootargs in the default machine config.
  • Flash drive start address configuration is non mandatory anymore (it will be automatically chosen).
  • Changed fork method of remote machines to also return the process pid.
  • Changed rebind method of remote machines to support binding on port 0 and return the bound address.
  • Proofs are now mandatory in access logs.
  • Disabled Sv57 virtual address translation mode from the RISC-V interpreter.
  • Omitted no4lvl from bootargs, it's not needed anymore.

Removed:

  • Removed dkjson Lua library (not needed since we have our own JSON encoding functions now).
  • Removed clua-htif.cpp (its constants are exposed along with other Lua constants now).
  • Removed GDB documentation from the repository (it was moved to the wiki).
  • Removed many dead code after the refactoring.
  • Removed machine-c-defines.h header (its constants are now gone or in the machine C API header).

C API Added:

  • Added cm_get_last_error_message, returning the error message for the most recent failed call.
  • Added public PMA enums with CMIO and RAM memory addresses.
  • Added cm_constant enumeration with new constants.
  • Added cm_cmio_yield_command and cm_cmio_yield_reason` enumeration with CMIO yields.
  • Added cm_access_log_type enumeration with bitwise flags for access log type.
  • Added cm_pma_constant enumeration.
  • Added CM_REG_* for all machine control registers.
  • Added CM_MCYCLE_MAX macro and set to UINT64_MAX
  • Added register views for HTIF fromhost/tohost DEV/CMD/REASON/DATA fields.
  • Added register views for IFLAGS PRV/X/Y/H fields.
  • Added new API cm_receive_cmio_request to make it easier to perform IO using CMIO protocol.

C API Removed:

  • Removed all err_msg arguments (the latest error message can be retrieved from cm_get_last_error_message()).
  • Removed all one_based arguments from log related functions (its error messages now use user ordinal numbers).
  • Removed machine runtime config arguments from verify functions (they were unused).
  • Removed cm_new_default_machine_config
  • Removed cm_get_f_address
  • Removed cm_get_x_address
  • Removed cm_reset_iflags_X/cm_set_iflags_H/cm_set_iflags_X
  • Removed cm_verify_*_log.
  • Removed all structs, complex data structures are passed as JSON strings now.
  • Removed all cm_read_*/cm_write_* of all registers, except for mcycle/uarch_cycle/uarch_halt_flag/iflags.
  • Removed most cm_delete_* functions.
  • Removed some CM_ERROR_* enums.

C API Renamed:

  • Renamed cm_read_csr -> cm_read_reg
  • Renamed cm_write_csr -> cm_write_reg
  • Renamed cm_get_csr_address -> cm_get_reg_address
  • Renamed cm_create_machine -> cm_create
  • Renamed cm_load_machine -> cm_load
  • Renamed cm_delete_machine -> cm_destroy
  • Renamed *_uarch_reset* -> *_reset_uarch*
  • Renamed *_uarch_step* -> *_step_uarch*
  • Renamed cm_verify_*_state_transition -> cm_verify_*
  • Renamed cm_machine_run -> cm_run
  • Renamed cm_machine_run_uarch -> cm_run_uarch
  • Renamed CM_PROC_CSR -> CM_REG
  • Renamed cm_create_jsonrpc_machine -> cm_jsonrpc_create_machine
  • Renamed cm_load_jsonrpc_machine -> cm_jsonrpc_load_machine
  • Renamed cm_get_jsonrpc_machine -> cm_jsonrpc_get_machine
  • Renamed cm_create_jsonrpc_mgr -> cm_jsonrpc_create_mgr
  • Renamed cm_delete_jsonrpc_mgr -> cm_jsonrpc_destroy_mgr
  • Renamed all enum types to use lowercase letters.

C API Changed:

  • Changed all C API functions to always return an error code and set last error message.
  • Changed all C API functions to always set output values, even on failure.
  • Enhanced generic read/write CSRs API to also support x, f and uarch registers.
  • Changed cm_replace_memory_range to take multiple arguments instead of a struct.
  • Changed cm_get_default_config output to its return instead of parameter (because it should never fail in practice).
  • Changed cm_get_memory_ranges to output a JSON string instead of a struct.
  • All functions now use only integers with fixed size (to simplify C binding generators).
  • Reorganized the C API into different categories in the header (making it easy for the reader to navigate through the API).
  • Changed error enumerations to use negative integers (to please rust bindgen).
  • Access log types are now defined by bitwise flags instead of a structure (for the remove structs goal).
  • Replaced all definitions with enums (because they are more friendly for binding generators).
  • Polished the C API documentation.

@edubart edubart self-assigned this Sep 8, 2024
@edubart edubart marked this pull request as draft September 8, 2024 22:17
@edubart edubart added the refactor Restructuring code, while not changing its original functionality label Sep 8, 2024
@edubart edubart added this to the v0.19.0 milestone Sep 8, 2024
@edubart edubart linked an issue Sep 8, 2024 that may be closed by this pull request
5 tasks
@edubart edubart force-pushed the refactor/c-api branch 2 times, most recently from 2f91fae to 4731564 Compare September 9, 2024 15:08
@edubart edubart force-pushed the refactor/c-api branch 3 times, most recently from 24bfd1e to 5fcce41 Compare September 10, 2024 21:02
@edubart edubart force-pushed the refactor/c-api branch 9 times, most recently from ec9390a to 0dc2566 Compare September 21, 2024 11:13
@edubart edubart force-pushed the refactor/c-api branch 3 times, most recently from e33bc44 to c684735 Compare September 29, 2024 15:13
@edubart edubart marked this pull request as ready for review September 30, 2024 10:18
@edubart edubart requested review from diegonehab and a team September 30, 2024 10:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactor Restructuring code, while not changing its original functionality
Projects
Status: Waiting Review
Development

Successfully merging this pull request may close these issues.

Simplify the C API
1 participant