11
11
void __cheri_compartment (" main" ) entry(void ) {
12
12
*MMIO_CAPABILITY (uint32_t , gpio) = 0xaa ;
13
13
printf (" Test\n " );
14
- mp_vminit ();
14
+ MicropythonContext ctx = MicropythonContext::create (0xc000 ).value ();
15
+ #ifdef TEST_COMPARMENT_ENTRIES
15
16
while (1 ) switch (mp_hal_stdin_rx_chr ()) {
16
17
#if MICROPY_ENABLE_COMPILER
17
18
case ' r' :
18
- if (!mp_friendly_repl ()) continue ;
19
+ if (!ctx. friendly_repl ()) continue ;
19
20
printf (" REPL exited with a failure\n " );
20
21
return ;
21
22
#endif
22
23
case ' s' :
23
- if (!mp_exec_str_single ("print('foo', 7*8*9, [ 'bar', 'baz' ])" )) continue ;
24
+ if (!ctx. exec_str_single (" print('foo', 7*8*9, [ 'bar', 'baz' ])" )) continue ;
24
25
printf (" String execution exited with a failure\n " );
25
26
case ' f' :
26
- if (!mp_exec_frozen_module ("frozentest.py" )) continue ;
27
+ if (!ctx. exec_frozen_module (" frozentest.py" )) continue ;
27
28
printf (" Frozen module exited with a failure\n " );
28
29
return ;
29
- break ;
30
- case 'q' :
31
- mp_vmexit ();
32
- printf ("VM Exited" );
30
+ case ' e' :
31
+ if (!ctx.exec_str_file (" def foo(a,b):\n print(a)\n return b*b\n print('created function `foo`')" )) {
32
+ std::optional<int > ret = ctx.exec_func <int >(" foo" , " bar" , 7 );
33
+ if (ret) {
34
+ printf (" Call to python `foo('bar', 7)` returned %d\n " , *ret);
35
+ continue ;
36
+ } else {
37
+ printf (" Call to python `foo('bar', 7)` failed.\n " );
38
+ return ;
39
+ }
40
+ } else {
41
+ printf (" File-mode string execution exited with a failure\n " );
42
+ return ;
43
+ }
44
+ case ' q' :
45
+ printf (" Exiting\n " );
33
46
return ;
34
47
}
48
+ #else
49
+ int ret = 0 ;
50
+ do {
51
+ while (!ret) ret = ctx.var_repl ();
52
+ printf (" soft reboot\r\n " );
53
+ ret = ctx.restart ();
54
+ } while (!ret);
55
+ printf (" Restart failed with code %d\n " , ret);
56
+ return ;
57
+ #endif
35
58
}
36
59
37
60
enum ErrorRecoveryBehaviour compartment_error_handler (struct ErrorState *frame, size_t mcause, size_t mtval) {
38
- static const char * const cheri_exc_code [32 ] = { "NONE " , "BOUNDS" , "TAG" , "SEAL" , "04" , "05" , "06" , "07" ,
61
+ static const char * const cheri_exc_code[32 ] = { " UNWIND " , " BOUNDS" , " TAG" , " SEAL" , " 04" , " 05" , " 06" , " 07" ,
39
62
" 08" , " 09" , " 0a" , " 0b" , " 0c" , " 0d" , " 0e" , " 0f" ,
40
63
" 10" , " EXEC" , " LOAD" , " STORE" , " 14" , " STCAP" , " STLOC" , " 17" ,
41
64
" SYSREG" , " 19" , " 1a" , " 1b" , " 1c" , " 1d" , " 1e" , " 1f" };
@@ -44,7 +67,7 @@ enum ErrorRecoveryBehaviour compartment_error_handler(struct ErrorState *frame,
44
67
45
68
if (mcause == 0x1c ) {
46
69
printf (" CHERI fault (type %s):\n " , cheri_exc_code[mtval & 0x1f ]);
47
- printf ("\tat %p\n" , frame -> pcc );
70
+ printf (" \t at %p in compartment 'main' \n " , frame->pcc );
48
71
if (mtval & 0x400 ) { // S=1, special register
49
72
switch (mtval >> 5 ) {
50
73
case 0x20 : printf (" \t on PCC\n " ); break ;
@@ -54,10 +77,13 @@ enum ErrorRecoveryBehaviour compartment_error_handler(struct ErrorState *frame,
54
77
case 0x3f : printf (" \t on MEPCC\n " ); break ;
55
78
default : printf (" on unknown special register %ld\n " , (mtval >> 5 ) & 0x1f );
56
79
}
57
- } else {
80
+ } else if (mtval) {
58
81
int errreg = mtval >> 5 ;
59
- void * errcap = frame -> registers [errreg - 1 ];
82
+ void * errcap = errreg ? frame->registers [errreg - 1 ] : NULL ;
60
83
printf (" \t on capability %p [base %lx, len %lx, perms %lx] in register c%s\n " , errcap, __builtin_cheri_base_get (errcap), __builtin_cheri_length_get (errcap), __builtin_cheri_perms_get (errcap), reg_names[errreg]);
84
+ } else {
85
+ printf (" \r\n\x04\x04\r\n " );
86
+ return InstallContext;
61
87
}
62
88
}
63
89
else {
0 commit comments