From 34e84f2926912934e252f10b7dbd2383cb4158e1 Mon Sep 17 00:00:00 2001 From: Akilan Date: Thu, 19 Mar 2026 12:26:29 +0000 Subject: [PATCH] sample C cheri program hybrid cap --- Tests/isa/CPrograms/main.c | 18 ++++++++++++++++ Tests/isa/CPrograms/start.S | 40 +++++++++++++++++++++++++++++++++++ Tests/isa/build-assembler.sh | 10 +++++++-- Tests/isa/cheri.S | 33 ----------------------------- Tests/isa/cheri.c | 20 ++++++++++++++++++ Tests/isa/testC | Bin 66408 -> 5048 bytes 6 files changed, 86 insertions(+), 35 deletions(-) create mode 100644 Tests/isa/CPrograms/main.c create mode 100644 Tests/isa/CPrograms/start.S create mode 100644 Tests/isa/cheri.c diff --git a/Tests/isa/CPrograms/main.c b/Tests/isa/CPrograms/main.c new file mode 100644 index 0000000..57bbe57 --- /dev/null +++ b/Tests/isa/CPrograms/main.c @@ -0,0 +1,18 @@ +#include +#include + +int main(void) { + void *__capability csp; + + // Set address + csp = (void *__capability)0x80001000; + + // Set bounds + csp = cheri_bounds_set(csp, 8); + // Increment offset + // csp = cheri_offset_increment(csp, 10); + + uint64_t val = *(uint64_t *__capability)csp; + + return 0; +} \ No newline at end of file diff --git a/Tests/isa/CPrograms/start.S b/Tests/isa/CPrograms/start.S new file mode 100644 index 0000000..4fd3e87 --- /dev/null +++ b/Tests/isa/CPrograms/start.S @@ -0,0 +1,40 @@ + .section .text + .globl _start + .align 4 +_start: + # cspecialr c1, pcc + + # Use csetaddr to build a stack capability + # li t0, 0x80001000 # fixed top-of-stack address + # csetaddr csp, ct0, t0 + + # bound it to 8 bytes (size of dword) + # li t2, 8 + # csetbounds csp, csp, t2 + # li t5, 4 + # cincoffsetimm csp, csp, 10 + + # Read bounds + # cgetbase t3, csp + # ld t0, 0(csp) + # derive capability from PCC + # csetaddr c1, ct0, t0 + + cspecialr ca0, pcc # Get the root data capability + li t0, 0x80002000 # Set top of stack address + csetaddr csp, ca0, t0 # Set address + li t1, -2048 # 2KB size + csetbounds csp, csp, t1 # Restrict bounds so main can't wander + + # Call main + # sw ra, 12(sp) + call main + + # a0 contains return value + mv t1, a0 + + # la t0, tohost + # Signal success + la t0, 0 + li t1, 1 + sd t1, 0(t0) \ No newline at end of file diff --git a/Tests/isa/build-assembler.sh b/Tests/isa/build-assembler.sh index b7870b2..e671298 100644 --- a/Tests/isa/build-assembler.sh +++ b/Tests/isa/build-assembler.sh @@ -1,8 +1,14 @@ # Send assembler file to remote machine to run -scp cheri.S home:/home/akilan/cheri/output/sdk/bin/ +scp Cprograms/start.S home:/home/akilan/cheri/output/sdk/bin/ +scp Cprograms/main.c home:/home/akilan/cheri/output/sdk/bin/ # Run compiled instruction remotely -ssh home 'cd /home/akilan/cheri/output/sdk/bin/ && ./clang --target=riscv64-unknown-elf -march=rv64gcxcheri -mabi=l64pc128 -nostdlib -nostartfiles -Wl,-Ttext=0x80000000 -o testC cheri.S' +# ssh home 'cd /home/akilan/cheri/output/sdk/bin/ && ./clang --target=riscv64-unknown-elf -march=rv64gcxcheri -mabi=l64pc128 -nostdlib -nostartfiles -Wl,-Ttext=0x80000000 -o testC start.S main.c' + +ssh home 'cd /home/akilan/cheri/output/sdk/bin/ && ./clang --target=riscv64-unknown-elf -march=rv64gcxcheri -mabi=lp64d -nostdlib -nostartfiles -Wl,-Ttext=0x80000000 -o testC start.S main.c' + +# Disassembly ouput +ssh home 'cd /home/akilan/cheri/output/sdk/bin/ && ./llvm-objdump -d testC' # Copy file back for testing scp home:/home/akilan/cheri/output/sdk/bin/testC . diff --git a/Tests/isa/cheri.S b/Tests/isa/cheri.S index 8db85d9..de84290 100644 --- a/Tests/isa/cheri.S +++ b/Tests/isa/cheri.S @@ -14,12 +14,6 @@ _start: # li t5, 4 cincoffsetimm csp, csp, 10 - # seal the bounded capability - # cspecialr ct1, 4 - # li t2, 12 - # csetaddr ct1, ct1, t2 - # ccseal c2, csp, t2 - # Read bounds cgetbase t3, csp ld t0, 0(csp) @@ -30,30 +24,3 @@ _start: li t1, 1 csd t1, 0(c1) -halt: - wfi - j halt - -.globl exit -exit: - j exit - - .section .bss - .align 16 -stack_bottom: - .space 2048 -stack_top: # address = 0x80001000 (for csetaddr) - -# -------------------------------------------------- -# Data section (NO .bss) -# -------------------------------------------------- - -# .section .data -# .align 3 - -# .globl tohost -# .globl fromhost -# tohost: -# .dword 0 -#fromhost: -# .dword 0 \ No newline at end of file diff --git a/Tests/isa/cheri.c b/Tests/isa/cheri.c new file mode 100644 index 0000000..fda1db2 --- /dev/null +++ b/Tests/isa/cheri.c @@ -0,0 +1,20 @@ +#include +#include + +int main() { + void *__capability csp; + + // Set address + csp = (void *__capability)0x80001000; + + // Set bounds + csp = cheri_bounds_set(csp, 8); + + // Increment offset + csp = cheri_offset_increment(csp, 10); + + uint64_t val = *(uint64_t *__capability)csp; + + while (1) {} + return 0; +} \ No newline at end of file diff --git a/Tests/isa/testC b/Tests/isa/testC index 95d69aea18d3ad1d8a1c383d46ee15360625df37..2a216ab2e4ab80a37539890baf8192988e03836d 100755 GIT binary patch literal 5048 zcmeHLJ#W)M7(Tmk+Ko^c%4Ql-l}dFS$92+*AtVh-r7W~XLdpuONGM6oqZQt7YV__a<7zo-p~7Zoawn=Yh2?w4p8I47eJyA z=gN3Nc62sI0YMp*fPmInw5CyG{ zfyT%SafmF$OAjx|DAK;`A7LBc^>1wk01w}@N6t^q?~(*X`if*9ix~rq0mcAhfHA-r zU<@z@7z2y}#sFi0G4MYbXbOOTGY`s7_$GHlJU0WvCZ}hfp(}j~MQ-KMgNjhizB0i2 zo%Ce!CY>zKvguz|H>%i<< zzAbw)0A^oz_r#&)2ll`f^F>LPWO2C*p%-lAa(g!H?i-RhaB|h%o$bxlYQ46-mFxG1 z&Z_4RdX@<#)FT$vwj#?N#W1urL)NrH!PHB|PDwNJ#(G{|vy6^b&{gd+XxQ#;%ijf#4j_;s0wopvrY7ycd7RKjR8qLWPZmneB$u89+~ z2(Y$7%2S{^3)NF-A*rieKn0vb|A7n5=_qj1R*4s&;S4c delta 740 zcmb_aJ4*vW5T3m}yo3<$5;YbY5+g>zJitN&g2oi-BzIsTDe}-jf=Xg;kraYa&{A^j zA27YBm_j=n|An<+n?kS>XLEak)(+gx?Du`MJ2yK|@%Kw-Zy{7C1h^4!zy_>>8&?_J z=6cjquox(qjg+S+Ha)YS+D?yB0BjG61#pdf6bdzvu3&_>c6fs@OQ*fjJvYEQ z>Pl~v~{Z{s{bS5!W7RzD<+?Os@H zaO4n|f8#TVD|ilCmoK)>VZiaFKR6_5twSMMH4JvEuEgt=0}2MM?CzJUO-h@GVndrP zC?VdaQ0p`(c012&W@~|Il4d)BSeQQwHeZ-sm3T>+71VKd6Y$DDOV(mr>Lki_rl~Uc@;w0Vr8whfxdF34gh$AJL!(gH=xSr4=2TuI#0htnb3cUjpJf9*VYhUZi M*i=T0@}f