Skip to content

Commit

Permalink
fix: Remove atomic operation support in CKB VM (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
xxuejie authored and doitian committed Jan 28, 2019
1 parent 1ee65ec commit af51e3a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
6 changes: 3 additions & 3 deletions rfcs/0003-ckb-vm/0003-ckb-vm.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ Created: 2018-08-01

VM layer in CKB is used to perform a series of validation rules to determine if transaction is valid given transaction's inputs and outputs.

CKB uses [RISC-V](https://riscv.org/) ISA to implement VM layer. To be more precise, CKB uses rv64imac archivecture: it is based on core [RV64I](https://riscv.org/specifications/) ISA with M standard extension for integer multiplication and division, A extension for atomic operations, and C standard extension for RCV(RISC-V Compressed Instructions). Note that for now, CKB doesn't support floating point instructions, this might be added in future versions if needed.
CKB uses [RISC-V](https://riscv.org/) ISA to implement VM layer. To be more precise, CKB uses rv64imc architecture: it is based on core [RV64I](https://riscv.org/specifications/) ISA with M standard extension for integer multiplication and division, and C standard extension for RCV(RISC-V Compressed Instructions). Note that CKB doesn't support floating point instructions, a CKB script developer can choose to pack a softfloat implementation into the binary if needed.

CKB relies on dynamic linking and syscalls to provide additional capabilities required by the blockchain, such as reading external cells or other crypto computations. Any compilers with RV64I support, such as [riscv-gcc](https://github.com/riscv/riscv-gcc), [riscv-llvm](https://github.com/lowRISC/riscv-llvm) or [Rust](https://github.com/rust-embedded/wg/issues/218) can be used to generate CKB compatible scripts.

## RISC-V Runtime Model

CKB leverages 64-bit RISC-V virtual machine to run contracts. We provide the core instructions in 64-bit address space, with additional integer multiplication/division extension instructions. Additional atomic extension operations are also provided for compatibility with Rust. CKB also supports RISC-V Compressed Instructions to reduce contract size. For maximum tooling and debugging support, CKB leverages Linux ELF format directly as contract format.
CKB leverages 64-bit RISC-V virtual machine to run contracts. We provide the core instructions in 64-bit address space, with additional integer multiplication/division extension instructions. CKB also supports RISC-V Compressed Instructions to reduce contract size. For maximum tooling and debugging support, CKB leverages Linux ELF format directly as contract format.

Each contract has a maximum size of 10MB in uncompressed size, and 1MB in gzip size. CKB virtual machine has a maximum of 128 MB runtime memory for running contracts. VM's runtime memory provides space for executable code pages mapped from contracts, stack space, head space and mmapped pages of external cell.

Expand All @@ -43,7 +43,7 @@ int main(int argc, char* argv[]) {
Contract starts from main function in the ELF formatted contract file, arguments are passed in via standard argc and argv. When main returns 0, the contract is treated as success. Note that due to space consideration, we might not store full inputs and outputs data in argv. Instead, we might just provide metadata in argv, and leverages additional libraries and syscalls to support input/output loading. This way the runtime cost can be minimized. CKB VM is a strict single-threaded model, contract can ship with coroutines of their own.
For simplicity, CKB doesn't support floating point numbers for now. Even though CKB only supports single thread, A standard extension for atomic operations is still provided due to the widespread use of rv64imac.
For simplicity and deterministic behavior, CKB doesn't support floating point numbers. We suggest a softfloat solution if floating point number is really needed. Since CKB runs in a single threaded environment, atomic instructions are not needed.
## Libraries and bootloader
Expand Down
6 changes: 3 additions & 3 deletions rfcs/0003-ckb-vm/0003-ckb-vm.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ Created: 2018-08-01

CKB 的 VM 层用于在给定 transaction 的 inputs 与 outputs 的情况下,执行一系列验证条件,以判断 transaction 是否合法并返回结果。

CKB 使用 [RISC-V](https://riscv.org/) 指令集来实现虚拟机层。更精确的说,CKB 使用 rv64imac 指令集架构:基于 [RV64I](https://riscv.org/specifications/) 核心指令集,并添加 RV32M 整型乘除法扩展,原子性内存操作,以及 RVC 指令压缩功能。注意目前 CKB 并不支持浮点数运算,如有需要将在未来版本中考虑引入
CKB 使用 [RISC-V](https://riscv.org/) 指令集来实现虚拟机层。更精确的说,CKB 使用 rv64imc 指令集架构:基于 [RV64I](https://riscv.org/specifications/) 核心指令集,并添加 RV32M 整型乘除法扩展以及 RVC 指令压缩功能。注意 CKB 不支持浮点数运算,合约开发者如有需要,可以通过添加 softfloat 实现来完成相应功能

CKB 通过动态链接库的方式,依赖 syscall 来实现链上运算所需的其他功能,比如读取 Cell 的内容,或是其他与 block 相关的普通运算及加密运算。任何支持 RV64I 的编译器 (如 [riscv-gcc](https://github.com/riscv/riscv-gcc), [riscv-llvm](https://github.com/lowRISC/riscv-llvm), [Rust](https://github.com/rust-embedded/wg/issues/218)) 生成的可执行文件均可以作为 CKB VM 中的 script 来运行。

## RISC-V 运行模型

CKB 中使用 64 位的 RISC-V 虚拟机作为 VM 来执行合约。VM 运行在 64 位地址空间下,提供了 RV32I 定义的核心指令集,RV64M 扩展中的整型乘除法的扩展指令,以及 RV64A 中的原子性内存操作指令。为减小生成的合约大小,CKB 还支持 RVC 指令压缩功能,尽可能减小指令的存储开销。合约会直接使用 Linux 的 ELF 可执行文件格式,以方便对接开源社区的工具及离线调试。
CKB 中使用 64 位的 RISC-V 虚拟机作为 VM 来执行合约。VM 运行在 64 位地址空间下,提供了 RV32I 定义的核心指令集,以及 RV64M 扩展中的整型乘除法的扩展指令。为减小生成的合约大小,CKB 还支持 RVC 指令压缩功能,尽可能减小指令的存储开销。合约会直接使用 Linux 的 ELF 可执行文件格式,以方便对接开源社区的工具及离线调试。

每个合约在 gzip 后最大提供 1MB 的存储空间,解压后的原始合约最大限制为 10 MB。合约运行时,CKB 虚拟机会为合约提供 128 MB 的运行空间,其中包含合约可执行文件映射到虚拟机上的代码页,合约运行时需要的栈空间,堆空间以及外部的 Cell 通过 mmap 映射后的地址页。

Expand All @@ -45,7 +45,7 @@ int main(int argc, char* argv[]) {
合约运行从合约 ELF 文件中的 main 函数开始执行,通过 argc 与 argv 提供输入参数进行合约的执行,当 main 函数返回值为 0 时,认为合约执行成功,否则合约执行失败。注意这里的 argc 与 argv 并不保存完整的 inputs 以及 outputs 数据,而是只保留相应的 metadata,对 inputs 与 outputs 的读取则通过单独定义的库与 syscalls 来实现,以便减少不必要的开销。同时 CKB VM 仅为单线程模型,合约文件可以自行提供 coroutine 实现,但是在 VM 层不提供 threading。
目前基于简化实现的考虑,CKB 并不提供浮点数运算。同时虽然 CKB 仅为单线程模型,但是考虑到 rv64imac 的广泛使用,仍旧提供 RV64A 扩展中的原子性操作
基于简化实现以及确定性的考虑,CKB 不提供浮点数运算。如果有对浮点数的需要,我们建议通过引入 softfloat 来实现需求。同时由于 CKB VM 仅为单线程模型,不提供对于原子性操作的支持
## 辅助库与 Bootloader
Expand Down

0 comments on commit af51e3a

Please sign in to comment.