@@ -8,10 +8,11 @@ use ckb_vm_definitions::{
8
8
RET_SLOWPATH , TRACE_ITEM_LENGTH , TRACE_SIZE ,
9
9
} ,
10
10
instructions:: OP_CUSTOM_TRACE_END ,
11
- ISA_MOP , MEMORY_FRAMES , MEMORY_FRAME_PAGE_SHIFTS , RISCV_GENERAL_REGISTER_NUMBER ,
12
- RISCV_PAGE_SHIFTS ,
11
+ ISA_MOP , MEMORY_FRAMES , MEMORY_FRAMESIZE , MEMORY_FRAME_PAGE_SHIFTS ,
12
+ RISCV_GENERAL_REGISTER_NUMBER , RISCV_MAX_MEMORY , RISCV_PAGE_SHIFTS ,
13
13
} ;
14
14
use rand:: { prelude:: RngCore , SeedableRng } ;
15
+ use std:: alloc:: { alloc, Layout } ;
15
16
use std:: os:: raw:: c_uchar;
16
17
17
18
use crate :: {
@@ -26,8 +27,8 @@ use crate::{
26
27
fill_page_data, get_page_indices, memset, round_page_down, round_page_up, FLAG_DIRTY ,
27
28
FLAG_EXECUTABLE , FLAG_FREEZED , FLAG_WRITABLE , FLAG_WXORX_BIT ,
28
29
} ,
29
- CoreMachine , DefaultMachine , Error , Machine , Memory , SupportMachine , MEMORY_FRAME_SHIFTS ,
30
- RISCV_PAGES , RISCV_PAGESIZE ,
30
+ CoreMachine , DefaultMachine , DefaultMachineRunner , Error , Machine , Memory , SupportMachine ,
31
+ MEMORY_FRAME_SHIFTS , RISCV_PAGES , RISCV_PAGESIZE ,
31
32
} ;
32
33
33
34
impl CoreMachine for Box < AsmCoreMachine > {
@@ -412,6 +413,56 @@ impl Memory for Box<AsmCoreMachine> {
412
413
}
413
414
414
415
impl SupportMachine for Box < AsmCoreMachine > {
416
+ fn new_with_memory (
417
+ isa : u8 ,
418
+ version : u32 ,
419
+ max_cycles : u64 ,
420
+ memory_size : usize ,
421
+ ) -> Box < AsmCoreMachine > {
422
+ assert_ne ! ( memory_size, 0 ) ;
423
+ assert_eq ! ( memory_size % RISCV_PAGESIZE , 0 ) ;
424
+ assert_eq ! ( memory_size % ( 1 << MEMORY_FRAME_SHIFTS ) , 0 ) ;
425
+
426
+ let mut machine = unsafe {
427
+ let machine_size =
428
+ std:: mem:: size_of :: < AsmCoreMachine > ( ) - RISCV_MAX_MEMORY + memory_size;
429
+
430
+ let layout = Layout :: array :: < u8 > ( machine_size) . unwrap ( ) ;
431
+ let raw_allocation = alloc ( layout) as * mut AsmCoreMachine ;
432
+ Box :: from_raw ( raw_allocation)
433
+ } ;
434
+ machine. registers = [ 0 ; RISCV_GENERAL_REGISTER_NUMBER ] ;
435
+ machine. pc = 0 ;
436
+ machine. next_pc = 0 ;
437
+ machine. running = 0 ;
438
+ machine. cycles = 0 ;
439
+ machine. max_cycles = max_cycles;
440
+ if cfg ! ( feature = "enable-chaos-mode-by-default" ) {
441
+ machine. chaos_mode = 1 ;
442
+ } else {
443
+ machine. chaos_mode = 0 ;
444
+ }
445
+ machine. chaos_seed = 0 ;
446
+ machine. load_reservation_address = u64:: MAX ;
447
+ machine. reset_signal = 0 ;
448
+ machine. version = version;
449
+ machine. isa = isa;
450
+ machine. flags = [ 0 ; RISCV_PAGES ] ;
451
+ for i in 0 ..TRACE_SIZE {
452
+ machine. traces [ i] = Trace :: default ( ) ;
453
+ }
454
+ machine. frames = [ 0 ; MEMORY_FRAMES ] ;
455
+
456
+ machine. memory_size = memory_size as u64 ;
457
+ machine. frames_size = ( memory_size / MEMORY_FRAMESIZE ) as u64 ;
458
+ machine. flags_size = ( memory_size / RISCV_PAGESIZE ) as u64 ;
459
+
460
+ machine. last_read_frame = u64:: MAX ;
461
+ machine. last_write_page = u64:: MAX ;
462
+
463
+ machine
464
+ }
465
+
415
466
fn cycles ( & self ) -> u64 {
416
467
self . cycles
417
468
}
@@ -473,34 +524,22 @@ pub struct AsmMachine {
473
524
pub machine : DefaultMachine < Box < AsmCoreMachine > > ,
474
525
}
475
526
476
- impl AsmMachine {
477
- pub fn new ( machine : DefaultMachine < Box < AsmCoreMachine > > ) -> Self {
478
- Self { machine }
479
- }
527
+ impl DefaultMachineRunner for AsmMachine {
528
+ type Inner = Box < AsmCoreMachine > ;
480
529
481
- pub fn set_max_cycles ( & mut self , cycles : u64 ) {
482
- self . machine . inner . max_cycles = cycles ;
530
+ fn new ( machine : DefaultMachine < Box < AsmCoreMachine > > ) -> Self {
531
+ Self { machine }
483
532
}
484
533
485
- pub fn load_program (
486
- & mut self ,
487
- program : & Bytes ,
488
- args : impl ExactSizeIterator < Item = Result < Bytes , Error > > ,
489
- ) -> Result < u64 , Error > {
490
- self . machine . load_program ( program, args)
534
+ fn machine ( & self ) -> & DefaultMachine < Box < AsmCoreMachine > > {
535
+ & self . machine
491
536
}
492
537
493
- pub fn load_program_with_metadata (
494
- & mut self ,
495
- program : & Bytes ,
496
- metadata : & ProgramMetadata ,
497
- args : impl ExactSizeIterator < Item = Result < Bytes , Error > > ,
498
- ) -> Result < u64 , Error > {
499
- self . machine
500
- . load_program_with_metadata ( program, metadata, args)
538
+ fn machine_mut ( & mut self ) -> & mut DefaultMachine < Box < AsmCoreMachine > > {
539
+ & mut self . machine
501
540
}
502
541
503
- pub fn run ( & mut self ) -> Result < i8 , Error > {
542
+ fn run ( & mut self ) -> Result < i8 , Error > {
504
543
if self . machine . isa ( ) & ISA_MOP != 0 && self . machine . version ( ) == VERSION0 {
505
544
return Err ( Error :: InvalidVersion ) ;
506
545
}
@@ -573,6 +612,30 @@ impl AsmMachine {
573
612
}
574
613
Ok ( self . machine . exit_code ( ) )
575
614
}
615
+ }
616
+
617
+ impl AsmMachine {
618
+ pub fn set_max_cycles ( & mut self , cycles : u64 ) {
619
+ self . machine . inner . max_cycles = cycles;
620
+ }
621
+
622
+ pub fn load_program (
623
+ & mut self ,
624
+ program : & Bytes ,
625
+ args : impl ExactSizeIterator < Item = Result < Bytes , Error > > ,
626
+ ) -> Result < u64 , Error > {
627
+ self . machine . load_program ( program, args)
628
+ }
629
+
630
+ pub fn load_program_with_metadata (
631
+ & mut self ,
632
+ program : & Bytes ,
633
+ metadata : & ProgramMetadata ,
634
+ args : impl ExactSizeIterator < Item = Result < Bytes , Error > > ,
635
+ ) -> Result < u64 , Error > {
636
+ self . machine
637
+ . load_program_with_metadata ( program, metadata, args)
638
+ }
576
639
577
640
pub fn step ( & mut self , decoder : & mut Decoder ) -> Result < ( ) , Error > {
578
641
// Decode only one instruction into a trace
0 commit comments