Cartesi 백서

Cartesi

원문

bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 5쪽 중 2쪽

ough projects such as Pinocchio [ Parno et al. 2013] and lib- snark [Ben-Sasson et al. 2013]. Meanwhile, zero-knowledge proofs have first appeared in blockchain technology with privacy coins, where shielded addresses protect the identities of parties involved in a transaction [ Miers et al. 2013; van Saberhangen 2013]. In the Metropolis (Byzantium) fork, Ethereum introduced zk-SNARKS primitives into its virtual machine and some libraries have already started to be developed [ Schaeffer 2018]. In this context, Cartesi can accelerate this development by bringing more mature off-chain implementations of zk-SNARKS, such as Pinocchio or libsnark, directly into the blockchain. Compilers and Virtual Machines Besides the scalability solu- tions mentioned above, several projects try to provide different languages for developers to write smart contracts in, such as Vyper, LLL, Bamboo, etc. Although these projects provide a welcome set of languages and tools for the development of smart contracts, they do not fully alleviate the most fundamental restrictions that currently hamper blockchain development. The main reason being that all these languages run on a free standing environment and therefore they do not offer the advantages that come with Cartesi’s underlying operating system, as described in section 3 below. 3 Cartesi Machine specification The Cartesi Machine is a self-contained and deterministic computa- tional model that can host modern operating systems. Real-world computations happen inside operating systems for good reasons. Developers are trained to use toolchains that operate at the highest possible abstraction level for any given job. These toolchains isolate them from irrelevant hardware details and even from the particulars of a given operating system. Inventing an ad-hoc new architecture would then require the porting of a toolchain and operating sys- tem. Instead, Cartesi Machines are based on a proven architecture for which a standard toolchain and operating system are already available. On the other hand, off-chain computations performed by Cartesi Machines must be verifiable by a blockchain. The blockchain must therefore host a reference implementation of the entire architec- ture. If it is ever to be trusted, this implementation must be easily auditable. To that end, both the architecture and the implementa- tion must be open and relatively simple. Together, these require- ments point to RISC-V . The RISC-V ISA is based on a minimal 32-bit integer instruction set to which several extensions can be added [Waterman and Asanovi´c 2017a]. Orthogonally, operand and address-space widths can be extended to 64-bits (or even 128-bits). Additionally, the standard defines a privileged architecture [ Water- man and Asanovi ´c 2017b] with features commonly used by modern operating systems, such as multiple privilege levels, paged-based 3 V ersion 1.01 Table 1: Instruction counts by extension. Entries in the form x + y refer to 32- and 64-bit variants of the same facility. Integer Mul/Div Atomics Privileged Total 47+12 8+5 11+11 5 71+28=99 Table 2: The processor state. Memory-mapped to the lowest 512 bytes in physical memory for external read-only access. Offset State Offset State 0x000 x0 0x160 misa 0x008 x1 0x168 mie ··· ··· 0x170 mip 0x0f8 x31 0x178 medeleg 0x100 pc 0x180 mideleg 0x108 mvendorid 0x188 mcounteren 0x110 marchid 0x190 stvec 0x118 mimplid 0x198 sscratch 0x120 mcycle 0x1a0 sepc 0x128 minstret 0x1a8 scause 0x130 mstatus 0x1b0 stval 0x138 mtvec 0x1b8 satp 0x140 mscratch 0x1c0 scounteren 0x148 mepc 0x1c8 ilrsc † 0x150 mcause 0x1d0 iflags † 0x158 mtval †Cartesi-specific state. virtual-memory, timers, interrupts, exceptions and traps, etc. Im- plementations are free to select the combination of extensions that better suit their needs. RISC-V was born of research in academia at UC Berkeley. It is now maintained by its own independent foundation. Larger corporations, including Google, Samsung, and Tesla, have recently joined forces with the project [Tilley 2018]. The platform is supported by a vibrant community of developers. Their efforts have produced an extensive software infrastructure, most notably ports of the Linux operating system and the GNU toolchain [ RISC-V 2018d]. It is important to keep in mind that RISC-V is not a toy architecture. It is suitable for direct native hardware implementation, which is indeed currently commercialized by SiFive Inc. This means that, in the future, Cartesi will not be limited to emulation or binary translation off-chain. The Cartesi Machine can be separated into a processor and a board. The processor performs the computations, executing the traditional fetch-execute loop while maintaining a variety of registers. The board defines the surrounding environment with an assortment of memories (ROM, RAM, flash) and devices. To make verification possible, Cartesi Machines map their entire state to physical mem- ory in a well-defined way. This includes the internal states of the processor, the board, and of all attached devices. Fortunately, this modification does not limit the operating system or the applications it hosts in any significant way. 3.1 The processor Following RISC-V terminology, Cartesi Machines implement the RV64IMASU ISA. The letters after RV specify the extension set. This selection corresponds to a 64-bit machine, Integer arithmetic with Multiplication and division, Atomic operations, as well as the optional Supervisor and User privilege levels. In addition, Cartesi Machines support the Sv48 mode of address translation and memory Figure 1: The iflags register gives the current privilege level, and specifies whether the machine is temporarily idle waiting for interrupts, or has been permanently halted. protection. Sv48 provides a 48-bit protected virtual address space, divided into 4KiB pages, organized by a four-level page table. This set of features creates a balanced compromise between the simplic- ity demanded by a blockchain implementation and the flexibility expected from off-chain computations. There are a total of 99 instructions, out of which 28 simply narrow or widen, respectively, their 64-bit or 32-bit counterparts. Table 1 breaks down the instruction count for each extension. This being a RISC ISA, most instructions are very simple and can be simulated in a few lines of high-level code. 2 In fact, the only complex operation is the virtual-to-physical address translation. Instruction decoding is particularly simple due to the reduced number of formats (only 4, all taking 32-bits). The entire processor state fits within 512 bytes, which are divided into 64 registers, each one holding 64-bits. Most of these registers are defined by the RISC-V ISA, and consist of 32 general-purpose integer registers and 26 control and status registers. The remaining are Cartesi-specific. The processor makes its entire state available, externally and read-only, by mapping individual registers to the lowest 512 bytes in physical memory. The adjacent 1.5KiB are reserved for future use. The entire mapping is given in table 2. The registers whose names start with i are Cartesi-specific, and have the following semantics. The layout for register iflags can be seen in figure 1. PRV gives the current privilege level, I is set to 1 when the processor is idle (i.e., waiting for interrupts), and H is set to 1 to signal the processor has been permanently halted. Register ilrsc holds the reservation address for the LR/SC atomic memory operations. Default initialization fills the state with the following values: • PRV in iflags is set to 3 (for the Machine privilege level); • misa is set to RV64IMASU; • SXL and UXL in mstatus are set to 2 (for 64-bits); • pc starts at 0x1000 (pointing to ROM); • marchid is set to cartesi￿ in ASCII. mvendorid is used to test for matching on-chain and off-chain implementations. mimplid is incremented with each update to a matching pair. The remaining default state is set to zero. 3.2 The board The interaction between board and processor happens through de- vices mapped to the processor’s physical address space. Table 3 shows this mapping. There are 64KiB of ROM starting at address 0x1000, where execution starts. The central role of this ROM is holding the devicetree [DTSpec 2017] describing the system hard- ware. In addition, a bootstrap program at ROM-base sets register x10 to 0 (the value of mhartid), x11 to point to the devicetree, and then jumps to RAM-base at 0x80000000. This is where the entry point of the boot image is expected to reside. Finally, a number of addi- tional physical memory ranges can be set aside for flash-memory devices. These will typically be preloaded with file-system images. 2The x86 ISA defines at least 2000 (potentially complex) instructions. 4 V ersion 1.01 Figure 2: Physical Memory Attributes. The istart and ilength of each range are aligned to a 4KiB boundary. The 12 LSBs of each 64-bit word give attributes for the range. Two non-memory devices are mapped to the address space. The Core Local Interruptor (or CLINT) controls the timer interrupt. The active addresses are 0x0200bff8 and 0x02004000, respectively mapped to registers mtime and mtimecmp. The CLINT issues a hardware interrupt whenever mtime equals mtimecmp. To ensure reproducibility, the processor’s clock and the timer are locked by a constant frequency divisor of 100. In other words, mtime is incre- mented once for every 100 increments of mcycle. The Host-Target Interface (HTIF) mediates communication with the external world. Its active addresses are 0x40000000 (tohost) and 0x40000008 (fromhost). It halts the machine when tohost is written to with bits 63–48 set to 0 and bit 0 set to 1. (Bits 47–1 can be set to an arbitrary exit code.) It also works as a rudimentary communications port during interactive sections. The physical memory mapping is described by Physical Memory Attribute records (PMAs). Each PMA consists of 2 64-bit words. The first word gives the start of a range and the second word its length. Since the ranges must be aligned to 4KiB page boundaries, the lowest 12-bits of each word are available for attributes. Figure 2 shows the meaning of each attribute field. The M, IO, and E bits are mutually exclusive, and respectively mark the range as memory, I/O mapped, or excluded. Bits R, W, and X grant read, write, and execute permissions, respectively. Finally, the IR and IW bits mark the range as idempotent for reads and writes, respectively. The board supports a total of 32 PMAs, and makes them available, read-only, starting at offset 2KiB in physical memory. Another 2KiB are reserved for future use. PMA 0 describes RAM, and PMAs 16– 23 describe flash devices 0–7. These PMAs are user-configurable during initialization and read-only thereafter. (The RAM istart field is hard-coded to 0x80000000.) Together, these records bound the maximum amount of storage accessible during computations. 3.3 The state transition function A computation is a sequence of machine states s0,s 1,...,s h, gov- erned by a transition function step such that si+1 = step(si). (1) Here, s0 is the initial state and sh is a halting state. The previous sections described the state space and the transition function for Cartesi Machines in great detail. Recall the state consists of the value of each word in the entire 64-bit address space of the Cartesi Machine. In practice, it takes vastly fewer than 264 bytes to represent a state. Only regions described in table 3 must be defined explicitly. All remaining values are implicitly filled with zeros. The RISC-V ISA manuals [ Waterman and Asanovi´c 2017a,b] spec- ify the state transitions corresponding to the execution of each in- struction. This means that states are well defined between executed instructions. Since all instructions can be implemented in O(1) time, Cartesi defines each state transition to take exactly 1 cycle. The index of a given state in the sequence can be read from the corresponding Table 3: Physical memory layout for a Cartesi Machine. Physical address Mapping 0x00000000–0x000003ff Processor shadow 0x00000800–0x00000Bff Board shadow 0x00001000–0x00010fff ROM (Bootstrap & Devicetree) 0x02000000–0x020bffff Core Local Interruptor 0x40000000–0x40007fff Host-Target Interface 0x80000000–* RAM *–* Flash 0 (Disk 0) ··· ··· *–* Flash 7 (Disk 7) value of mcycle. (Note that, since the machine can be occasionally idle, minstret does not track mcycle.) The only salient Cartesi-specific modification pertains to the halting of the machine. When field H in iflags is set to 1, no further state transitions are allowed. The condition is set explicitly when HTIF is instructed to halt the machine. 3.4 The Linux port Setting up a Linux system from scratch involves a variety of steps. Unlike stand-alone systems, embedded systems are not usually self- hosting. Instead, components are built in a separate host system, on which a cross-compiling toolchain for the target architecture has been installed. The key components are the GNU Compiler Collection and the GNU C Library. This infrastructure can be found in the RISC-V GNU toolchain repository [ RISC-V 2018a]. Building this infrastructure is the first step. The toolchain can then be used to cross-compile the Linux kernel. Kernel sources can be found in the RISC-V Linux repository [ RISC- V 2018b]. The kernel runs in supervisor mode, on top of a Supervisor Binary Interface (SBI) provided by a machine-mode shim: the Berke- ley Boot Loader (BBL). BBL can be found in the RISC-V Proxy Kernel repository [RISC-V 2018e]. The BBL is linked against the Linux kernel and this resulting boot image is preloaded into RAM. The SBI provides a simple interface through which the kernel inter- acts with CLINT and HTIF. Besides implementing the SBI, the BBL also installs a trap that catches invalid instruction exceptions. This mechanism can be used to emulate floating-point instructions (See section 4.3). After installing the trap, BBL switches to supervisor mode and cedes control to the kernel entry point. The final step is the creation of a root file-system. This process starts with a skeleton directory in the host system containing a few subdirectories (sbin, lib, var, etc) and text files ( sbin/init, etc/fstab, etc/passwd etc). Tiny versions of many common UNIX utilities ( ls, cd, rm, etc) can be combined into a single binary [Vlasenko 2018]. Target executables often depend on shared libraries provided by the toolchain (lib/libm.so, lib/ld.so, and lib/libc.so). Naturally, these libraries must be copied to the root file-system. Once the root directory is ready, it is copied into an actual file-system image (e.g., using gene2fs). These steps can be automated. Cartesi’s SDK makes a preconfigured host environment available to developers in the convenient form of a Docker container. Complex Linux systems can be built with the help of Sifive’s fork of Buildroot [ Petazzoni 2018], or RISC-V’s port of the Y octo project [RISC-V 2018c]. The environment in the container enables developers to customize the boot image and the root file- system according to the needs of their applications. Thousands of packages are available for installation. 5 V ersion 1.01 memory@80000000 { device_type = "memory"; reg = <0x0 0x80000000 0x0 0x8000000>; }; flash@8000000000 { #address-cells = <0x2>; #size-cells = <0x2>; compatible = "mtd-ram"; bank-width = <0x4>; reg = <0x80 0x0 0x0 0x4000000>; fs0@0 { label = "root"; reg = <0x0 0x0 0x0 0x4000000>; }; }; chosen { bootargs = "root=/dev/mtdblock0 rw"; }; Figure 3: Partial devicetree for a simple setup with 128MiB of RAM and a 64MiB flash device to be mounted as the root file-system. The root file-system image is installed as a flash device. Additional flash devices can be used to store the inputs to the computation, or to receive its outputs. The devicetree in ROM is used to inform Linux of the location of each flash device, the amount of RAM, and any kernel parameters. Figure 3 shows the relevant devicetree snippet. The first section specifies 128MiB of RAM starting at the 2GiB boundary. The middle section adds a 64MiB flash device, starting at the 512GiB boundary. The mtd-ram driver exposes the device as /dev/mtdblock0 under Linux’s virtual file-system. The last section, giving the kernel parameters bootargs, specifies the device to be mounted as root. After completing its own initialization, the kernel eventually cedes control to /sbin/init. In Cartesi DApps, this is typically a shell script that invokes the appropriate sequence of commands for per- forming the desired computation. The kernel passes to /sbin/init as command-line parameters all arguments after the separator ￿-￿ in bootargs. These can be used to define additional parameters for the computation to be performed. Upon completion, /sbin/init uses HTIF to halt the machine with an optional exit code. This can be used as part of the computation output. Arbitrarily complex inputs, parameters, and outputs can be passed as flash devices. 4 Cartesi Machines off-chain Off-chain implementations of Cartesi Machines serve two purposes. Their main role is the execution of the computation itself. The sec- ondary role is supporting the settlement of disputes over the results of computations. To provide these services, off-chain implementa- tions of Cartesi Machines must expose a programmable interface. 4.1 The scripting interface The instantiation of a machine can only happen after the initial values for its entire physical address space have been defined. The physical memory layout is parameterized by the total amount of RAM, and by the starts and lengths of all flash devices. PMAs are automatically initialized from these parameters. The initial contents of RAM (e.g., with the boot image) and of the flash devices (e.g., the root file system), are given by backing files. The backing files for flash devices can be shared, in which case modifications to physical memory are saved to the file mapped to the corresponding memory location. V alues that are not explicitly defined are default-initialized. In particular, the devicetree and the bootstrap in ROM can be either m=m a c h i n e { ram { ilength = 0x8000000, backing = "boot-image.bin" }, rom = { bootargs = "root=/dev/mtdblock0 rw", } flash0 = { label = "root", istart = 0x8000000000, ilength = 0x4000000, backing = "root-file-system.bin", shared = true } } Figure 4: The initialization of the off-chain machine automatically generates the devicetree of figure 3. filled automatically or loaded from a backing file. Initialization returns a machine handle that can be manipulated thereafter. machine =m a c h i n e { processor = processor , rom = rom , ram = ram , flash0 = drive , ... flash7 = drive , clint = clint , htif = htif , } processor ::= { x0 = word , x1 = word , ... pc = word }|{ backing = path } rom ::= { bootargs = string }|{ backing = path } ram ::= { ilength = word , backing = path } drive ::= { istart = word , ilength = word , backing = path , shared = bool , label = string } clint ::= { mtime = word , mtimecmp = word }|{ backing = path } htif ::= { fromhost = word , tohost = word }|{ backing = path } Figure 4 shows a sample machine call for the devicetree in figure 3. The machine runs until mcycle exceeds a limit value. The func- tion then returns false if the machine is halted, or true otherwise. bool = machine :run{limit = word } The machine can create a snapshot of its current state. This is a lightweight operation. It causes subsequent calls to run to modify the state under a copy-on-write policy. At a future time, a rollback operation can restore the snapshot state. Restoring the state lifts the copy-on-write policy. Consecutive calls to snapshot cause the previously snapshot state to be committed to memory. Calls to rollback without a previously snapshot state have no effect: machine :snapshot() machine :rollback() The machine provides read-only access to its memory contents. To that end, invoking word(address) returns the value of a given 64-b
프로젝트로 돌아가기