1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
#include <stdio.h>
#include <string.h>
#include <libmcpu.h>
#include "../utils/mcpu-asm.h"
#define CHECK(c,m) do{if(!(c)){fprintf(stderr,"FAIL: %s\n",m);return 1;}}while(0)
static const char src[]=
".control\n"
"ctl:\n"
" .integer 64, 10\n"
".input\n"
"stimulus:\n"
" .integer 64, 32\n"
".output\n"
"answer:\n"
" .integer 64, 0\n"
".trace\n"
"sample:\n"
" .integer 64, 0\n"
".text\n"
"_life:\n"
" LEA r64@0x00000, ctl\n"
" LEA r64@0x00008, stimulus\n"
" LEA r64@0x00010, answer\n"
" LEA r64@0x00018, sample\n"
" LOADP r64@0x00020, r64@0x00000\n"
" LOADP r64@0x00028, r64@0x00008\n"
" IADD r64@0x00030, r64@0x00020, r64@0x00028\n"
" STOREP r64@0x00010, r64@0x00030\n"
" STOREP r64@0x00018, r64@0x00030\n"
" HALT\n";
static __mpu_uint64_t
r64( unsigned a )
{
return( __cpu_data_get_u64( __cpu_reg_ptr( 64, a ) ) );
}
static void
regop( cpu_operand *o, unsigned bits, unsigned addr )
{
memset( o, 0, sizeof( *o ) );
o->type = CPU_OPERAND_REGISTER;
o->value.reg.bits = bits;
o->value.reg.address = addr;
}
int
main( void )
{
cpu_asm_image im;
cpu_instruction x;
char err[256];
__mpu_uint64_t p;
int rc;
__cpu_init();
CHECK( CPU_PTR_SEG_CONTROL == 3, "CONTROL selector" );
CHECK( CPU_PTR_SEG_INPUT == 4, "INPUT selector" );
CHECK( CPU_PTR_SEG_OUTPUT == 5, "OUTPUT selector" );
CHECK( CPU_PTR_SEG_TRACE == 6, "TRACE selector" );
CHECK( CPU_PTR_SEG_HEAP == 7, "HEAP selector" );
CHECK( CPU_PTR_SEG_FIRST_RESERVED == 8, "reserved selectors begin at 8" );
CHECK( cpu_assemble_text( src, &im, err, sizeof( err ) ) == 0, err );
CHECK( im.control_size == 8 && im.input_size == 8 && im.output_size == 8 && im.trace_size == 8, "model segment image sizes" );
CHECK( im.has_life && im.life_entry == 0, "_life entry metadata" );
CHECK( __cpu_program_set( im.code, im.code_size ) == 0, "program set" );
CHECK( __cpu_data_set( im.data, im.data_size ) == 0, "data set" );
CHECK( __cpu_control_set( im.control, im.control_size ) == 0, "control set" );
CHECK( __cpu_input_set( im.input, im.input_size ) == 0, "input set" );
CHECK( __cpu_output_set( im.output, im.output_size ) == 0, "output set" );
CHECK( __cpu_trace_set( im.trace, im.trace_size ) == 0, "trace set" );
CHECK( __cpu_stack_alloc( 256 ) == 0, "stack alloc" );
CHECK( __cpu_heap_alloc( 128, 1024 ) == 0, "heap alloc" );
__cpu_pc = im.life_entry;
CHECK( __cpu_run() == CPU_EXEC_SUCCESS, "segment program run" );
CHECK( __cpu_ptr_segment( r64( 0x00 ) ) == CPU_PTR_SEG_CONTROL, "LEA CONTROL" );
CHECK( __cpu_ptr_segment( r64( 0x08 ) ) == CPU_PTR_SEG_INPUT, "LEA INPUT" );
CHECK( __cpu_ptr_segment( r64( 0x10 ) ) == CPU_PTR_SEG_OUTPUT, "LEA OUTPUT" );
CHECK( __cpu_ptr_segment( r64( 0x18 ) ) == CPU_PTR_SEG_TRACE, "LEA TRACE" );
CHECK( __cpu_data_get_u64( __cpu_output_ptr() ) == 42, "OUTPUT guest write" );
CHECK( __cpu_data_get_u64( __cpu_trace_ptr() ) == 42, "TRACE guest write" );
memset( &x, 0, sizeof( x ) );
x.opcode = CPU_OP_STOREP;
regop( &x.operand[0], 64, 0x00 );
regop( &x.operand[1], 64, 0x30 );
CHECK( __cpu_exec( &x ) == CPU_EXEC_MEMORY_FAULT, "CONTROL is guest read-only" );
regop( &x.operand[0], 64, 0x08 );
CHECK( __cpu_exec( &x ) == CPU_EXEC_MEMORY_FAULT, "INPUT is guest read-only" );
CHECK( __cpu_ptr_make( &p, CPU_PTR_SEG_HEAP, CPU_HEAP_BEGIN ) == 0, "make HEAP pointer" );
__cpu_data_put_u64( __cpu_reg_ptr( 64, 0x38 ), p );
regop( &x.operand[0], 64, 0x38 );
CHECK( __cpu_exec( &x ) == CPU_EXEC_MEMORY_FAULT, "HEAP is protected above initial _end break" );
CHECK( __cpu_heap_size() == 128 && __cpu_heap_limit() == 1024, "HEAP size/limit ABI" );
CHECK( __cpu_ptr_make( &p, CPU_PTR_SEG_HEAP, 0 ) == 0, "make HEAP guard pointer" );
__cpu_data_put_u64( __cpu_reg_ptr( 64, 0x38 ), p );
CHECK( __cpu_exec( &x ) == CPU_EXEC_MEMORY_FAULT, "HEAP low guard rejects access" );
CHECK( __cpu_ptr_make( &p, CPU_PTR_SEG_FIRST_RESERVED, 0 ) == 0, "make reserved pointer" );
__cpu_data_put_u64( __cpu_reg_ptr( 64, 0x38 ), p );
rc = __cpu_exec( &x );
CHECK( rc == CPU_EXEC_POINTER_FAULT, "reserved selector still faults" );
cpu_asm_image_free( &im );
__cpu_free_context();
puts( "libcpu model-segment/HEAP regression: PASS" );
return( 0 );
}
|