2024-05-23 15:32:54 +02:00
|
|
|
/* AMD64 ilo, (c) 2023 Christopher Leonard, MIT License */
|
2024-10-28 15:34:47 +01:00
|
|
|
/* Adaptions to use libc on OpenBSD by Charles Childers */
|
2024-05-23 15:32:54 +02:00
|
|
|
|
2024-10-14 17:58:17 +02:00
|
|
|
.global main
|
|
|
|
.extern open, read, write, close, exit, lseek
|
2024-10-28 15:34:47 +01:00
|
|
|
.extern unveil, pledge
|
2024-05-23 15:32:54 +02:00
|
|
|
|
|
|
|
/* rax: top of stack */
|
|
|
|
/* rbx: data stack */
|
|
|
|
/* rbp: jump table address */
|
|
|
|
/* r12: address stack */
|
|
|
|
/* r13: instruction pointer */
|
|
|
|
/* r14: opcode shift register */
|
|
|
|
/* r15: memory */
|
|
|
|
|
|
|
|
.bss
|
|
|
|
|
|
|
|
.align 8
|
|
|
|
blocks: .skip 8 /* name of blocks file (ilo.blocks) */
|
|
|
|
rom: .skip 8 /* name of image (ilo.rom) */
|
|
|
|
dstack: .skip 32*4
|
|
|
|
astack: .skip 256*4
|
|
|
|
a: .skip 4 /* other variables for misc. purposes */
|
|
|
|
b: .skip 4
|
|
|
|
f: .skip 4
|
|
|
|
s: .skip 4
|
|
|
|
d: .skip 4
|
|
|
|
l: .skip 4
|
|
|
|
memory: .skip 65536*4
|
|
|
|
|
|
|
|
.section .rodata
|
|
|
|
|
|
|
|
default_blocks:
|
|
|
|
.asciz "ilo.blocks"
|
|
|
|
default_rom:
|
|
|
|
.asciz "ilo.rom"
|
2024-10-28 15:34:47 +01:00
|
|
|
pledges:
|
|
|
|
.asciz "stdio rpath wpath tty"
|
|
|
|
mode_rw:
|
|
|
|
.asciz "rw"
|
2024-05-23 15:32:54 +02:00
|
|
|
|
|
|
|
io_table:
|
|
|
|
.byte 0
|
|
|
|
.byte iob-ioa
|
|
|
|
.byte ioc-ioa
|
|
|
|
.byte iod-ioa
|
|
|
|
.byte ioe-ioa
|
|
|
|
.byte iof-ioa
|
|
|
|
.byte iog-ioa
|
|
|
|
.byte ioh-ioa
|
|
|
|
|
|
|
|
.text
|
|
|
|
|
2024-10-14 17:58:17 +02:00
|
|
|
rdonly:
|
|
|
|
mov $0, %esi /* O_RDONLY */
|
|
|
|
call open
|
2024-05-23 15:32:54 +02:00
|
|
|
mov %eax, %edi
|
|
|
|
ret
|
|
|
|
|
2024-10-14 17:58:17 +02:00
|
|
|
wronly:
|
2024-05-23 15:32:54 +02:00
|
|
|
mov $1, %esi /* O_WRONLY */
|
2024-10-14 17:58:17 +02:00
|
|
|
call open
|
2024-05-23 15:32:54 +02:00
|
|
|
mov %eax, %edi
|
|
|
|
ret
|
|
|
|
|
|
|
|
load_image:
|
|
|
|
mov rom(%rip), %rdi
|
|
|
|
call rdonly
|
|
|
|
or %eax, %eax
|
|
|
|
jz 1f
|
|
|
|
mov %r15, %rsi
|
|
|
|
mov $65536 * 4, %edx
|
2024-10-14 17:58:17 +02:00
|
|
|
call read
|
2024-05-23 15:32:54 +02:00
|
|
|
call close
|
|
|
|
xor %eax, %eax
|
|
|
|
lea dstack-4(%rip), %rbx
|
|
|
|
lea astack-4(%rip), %r12
|
|
|
|
xor %r13d, %r13d
|
|
|
|
1: ret
|
|
|
|
|
|
|
|
save_image:
|
|
|
|
push %rax
|
|
|
|
mov rom(%rip), %rdi
|
|
|
|
call wronly
|
|
|
|
or %eax, %eax
|
|
|
|
jz 1f
|
|
|
|
mov %r15, %rsi
|
|
|
|
mov $65536 * 4, %edx
|
2024-10-14 17:58:17 +02:00
|
|
|
call write
|
2024-05-23 15:32:54 +02:00
|
|
|
call close
|
|
|
|
1: pop %rax
|
|
|
|
ret
|
|
|
|
|
2024-10-14 17:58:17 +02:00
|
|
|
block_read:
|
2024-10-28 15:34:47 +01:00
|
|
|
mov (%rbx), %esi
|
|
|
|
shl $12, %esi
|
|
|
|
xor %edx, %edx
|
|
|
|
mov %rax, %rdi
|
|
|
|
call lseek
|
|
|
|
mov $4096, %edx
|
|
|
|
lea (%r15, %r8, 4), %rsi
|
|
|
|
call read
|
|
|
|
mov -4(%rbx), %esi
|
|
|
|
sub $8, %rbx
|
|
|
|
ret
|
2024-10-14 17:58:17 +02:00
|
|
|
|
|
|
|
block_write:
|
2024-10-28 15:34:47 +01:00
|
|
|
mov (%rbx), %esi
|
|
|
|
shl $12, %esi
|
|
|
|
xor %edx, %edx
|
|
|
|
mov %rax, %rdi
|
|
|
|
call lseek
|
|
|
|
mov $4096, %edx
|
|
|
|
lea (%r15, %r8, 4), %rsi
|
|
|
|
call write
|
|
|
|
mov -4(%rbx), %esi
|
|
|
|
sub $8, %rbx
|
|
|
|
ret
|
2024-05-23 15:32:54 +02:00
|
|
|
|
|
|
|
.align 32
|
|
|
|
table: ret
|
|
|
|
.align 32
|
|
|
|
li: add $4, %rbx
|
|
|
|
inc %r13d
|
|
|
|
mov %eax, (%rbx)
|
|
|
|
mov (%r15,%r13,4), %eax
|
|
|
|
ret
|
|
|
|
.align 32
|
|
|
|
du: add $4, %rbx
|
|
|
|
mov %eax, (%rbx)
|
|
|
|
ret
|
|
|
|
.align 32
|
|
|
|
dr: mov (%rbx), %eax
|
|
|
|
sub $4, %rbx
|
|
|
|
ret
|
|
|
|
.align 32
|
|
|
|
sw: xchg (%rbx), %eax
|
|
|
|
ret
|
|
|
|
.align 32
|
|
|
|
pu: add $4, %r12
|
|
|
|
mov %eax, (%r12)
|
|
|
|
mov (%rbx), %eax
|
|
|
|
sub $4, %rbx
|
|
|
|
ret
|
|
|
|
.align 32
|
|
|
|
po: add $4, %rbx
|
|
|
|
mov %eax, (%rbx)
|
|
|
|
mov (%r12), %eax
|
|
|
|
sub $4, %r12
|
|
|
|
ret
|
|
|
|
.align 32
|
|
|
|
ju: lea -1(%eax), %r13d
|
|
|
|
mov (%rbx), %eax
|
|
|
|
sub $4, %rbx
|
|
|
|
ret
|
|
|
|
.align 32
|
|
|
|
ca: add $4, %r12
|
|
|
|
mov %r13d, (%r12)
|
|
|
|
lea -1(%eax), %r13d
|
|
|
|
mov (%rbx), %eax
|
|
|
|
sub $4, %rbx
|
|
|
|
ret
|
|
|
|
.align 32
|
|
|
|
cc: cmpl $0, (%rbx)
|
|
|
|
jz 1f
|
|
|
|
add $4, %r12
|
|
|
|
mov %r13d, (%r12)
|
|
|
|
lea -1(%eax), %r13d
|
|
|
|
1: mov -4(%rbx), %eax
|
|
|
|
sub $8, %rbx
|
|
|
|
ret
|
|
|
|
.align 32
|
|
|
|
cj: cmpl $0, (%rbx)
|
|
|
|
jz 1f
|
|
|
|
lea -1(%eax), %r13d
|
|
|
|
1: mov -4(%rbx), %eax
|
|
|
|
sub $8, %rbx
|
|
|
|
ret
|
|
|
|
.align 32
|
|
|
|
re: mov (%r12), %r13d
|
|
|
|
sub $4, %r12
|
|
|
|
ret
|
|
|
|
.align 32
|
|
|
|
eq: cmp %eax, (%rbx)
|
|
|
|
sete %al
|
|
|
|
movzbl %al, %eax
|
|
|
|
neg %eax
|
|
|
|
sub $4, %rbx
|
|
|
|
ret
|
|
|
|
.align 32
|
|
|
|
ne: cmp %eax, (%rbx)
|
|
|
|
setne %al
|
|
|
|
movzbl %al, %eax
|
|
|
|
neg %eax
|
|
|
|
sub $4, %rbx
|
|
|
|
ret
|
|
|
|
.align 32
|
|
|
|
lt: cmp %eax, (%rbx)
|
|
|
|
setl %al
|
|
|
|
movzbl %al, %eax
|
|
|
|
neg %eax
|
|
|
|
sub $4, %rbx
|
|
|
|
ret
|
|
|
|
.align 32
|
|
|
|
gt: cmp %eax, (%rbx)
|
|
|
|
setg %al
|
|
|
|
movzbl %al, %eax
|
|
|
|
neg %eax
|
|
|
|
sub $4, %rbx
|
|
|
|
ret
|
|
|
|
.align 32
|
|
|
|
fe: mov (%r15,%rax,4), %eax
|
|
|
|
ret
|
|
|
|
.align 32
|
|
|
|
st: mov (%rbx), %ecx
|
|
|
|
mov %ecx, (%r15,%rax,4)
|
|
|
|
mov -4(%rbx), %eax
|
|
|
|
sub $8, %rbx
|
|
|
|
ret
|
|
|
|
.align 32
|
|
|
|
ad: add (%rbx), %eax
|
|
|
|
sub $4, %rbx
|
|
|
|
ret
|
|
|
|
.align 32
|
|
|
|
su: sub (%rbx), %eax
|
|
|
|
neg %eax
|
|
|
|
sub $4, %rbx
|
|
|
|
ret
|
|
|
|
.align 32
|
|
|
|
mu: mull (%rbx)
|
|
|
|
sub $4, %rbx
|
|
|
|
ret
|
|
|
|
.align 32
|
|
|
|
di: mov %eax, %ecx
|
|
|
|
mov (%rbx), %eax
|
|
|
|
cdq
|
|
|
|
idiv %ecx
|
|
|
|
mov %edx, (%rbx)
|
|
|
|
ret
|
|
|
|
.align 32
|
|
|
|
an: and (%rbx), %eax
|
|
|
|
sub $4, %rbx
|
|
|
|
ret
|
|
|
|
.align 32
|
|
|
|
or: or (%rbx), %eax
|
|
|
|
sub $4, %rbx
|
|
|
|
ret
|
2024-10-14 17:58:17 +02:00
|
|
|
.align 32
|
2024-05-23 15:32:54 +02:00
|
|
|
xo: xor (%rbx), %eax
|
|
|
|
sub $4, %rbx
|
|
|
|
ret
|
|
|
|
.align 32
|
|
|
|
sl: mov %eax, %ecx
|
|
|
|
mov (%rbx), %eax
|
|
|
|
sub $4, %rbx
|
|
|
|
shl %cl, %eax
|
|
|
|
ret
|
|
|
|
.align 32
|
|
|
|
sr: mov %eax, %ecx
|
|
|
|
mov (%rbx), %eax
|
|
|
|
sub $4, %rbx
|
|
|
|
sar %cl, %eax
|
|
|
|
ret
|
|
|
|
.align 32
|
|
|
|
cp: mov %eax, %ecx
|
|
|
|
mov (%rbx), %edi
|
|
|
|
mov -4(%rbx), %esi
|
|
|
|
sub $8, %rbx
|
|
|
|
lea (%r15,%rdi,4), %rdi
|
|
|
|
lea (%r15,%rsi,4), %rsi
|
|
|
|
cmp %eax, %eax
|
|
|
|
repe cmpsd
|
|
|
|
sete %al
|
|
|
|
movzbl %al, %eax
|
|
|
|
neg %eax
|
|
|
|
ret
|
|
|
|
.align 32
|
|
|
|
cy: mov %eax, %ecx
|
|
|
|
mov (%rbx), %edi
|
|
|
|
mov -4(%rbx), %esi
|
|
|
|
mov -8(%rbx), %eax
|
|
|
|
sub $12, %rbx
|
|
|
|
lea (%r15,%rdi,4), %rdi
|
|
|
|
lea (%r15,%rsi,4), %rsi
|
|
|
|
repe movsd
|
|
|
|
ret
|
|
|
|
.align 32
|
|
|
|
io: mov %eax, %ecx
|
|
|
|
mov (%rbx), %eax
|
|
|
|
sub $4, %rbx
|
|
|
|
cmp $7, %ecx
|
|
|
|
ja 1f
|
|
|
|
lea io_table(%rip), %rdx
|
|
|
|
movzbl (%rdx,%rcx), %ecx
|
|
|
|
lea ioa(%rip), %rdx
|
|
|
|
add %rdx, %rcx
|
|
|
|
jmp *%rcx
|
|
|
|
1: ret
|
|
|
|
|
|
|
|
ioa: push %rax
|
2024-10-28 15:34:47 +01:00
|
|
|
mov %rsp, %rsi
|
|
|
|
mov $1, %edx
|
|
|
|
mov $1, %edi
|
2024-10-14 17:58:17 +02:00
|
|
|
call write
|
2024-05-23 15:32:54 +02:00
|
|
|
pop %rax
|
|
|
|
mov (%rbx), %eax
|
|
|
|
sub $4, %rbx
|
|
|
|
ret
|
2024-10-14 17:58:17 +02:00
|
|
|
|
2024-10-28 15:34:47 +01:00
|
|
|
iob: add $4, %rbx
|
2024-05-23 15:32:54 +02:00
|
|
|
mov %eax, (%rbx)
|
2024-10-28 15:34:47 +01:00
|
|
|
xor %edi, %edi
|
|
|
|
lea a(%rip), %rsi
|
|
|
|
mov $1, %edx
|
2024-10-14 17:58:17 +02:00
|
|
|
call read
|
2024-10-28 15:34:47 +01:00
|
|
|
movzx a(%rip), %eax
|
2024-05-23 15:32:54 +02:00
|
|
|
ret
|
2024-10-14 17:58:17 +02:00
|
|
|
|
2024-05-23 15:32:54 +02:00
|
|
|
ioc: mov %eax, %r8d
|
|
|
|
mov blocks(%rip), %rdi
|
2024-10-14 17:58:17 +02:00
|
|
|
mov $0, %rsi
|
|
|
|
mov $0, %rdx
|
|
|
|
call open /* open file (read-only) */
|
|
|
|
call block_read
|
2024-05-23 15:32:54 +02:00
|
|
|
call close
|
|
|
|
mov %esi, %eax
|
|
|
|
ret
|
2024-10-14 17:58:17 +02:00
|
|
|
|
2024-05-23 15:32:54 +02:00
|
|
|
iod: mov %eax, %r8d
|
|
|
|
mov blocks(%rip), %rdi
|
2024-10-14 17:58:17 +02:00
|
|
|
mov $1, %rsi
|
|
|
|
mov $0, %rdx
|
|
|
|
call open /* open file (write-only) */
|
|
|
|
call block_write
|
2024-05-23 15:32:54 +02:00
|
|
|
call close
|
|
|
|
mov %esi, %eax
|
|
|
|
ret
|
2024-10-14 17:58:17 +02:00
|
|
|
|
2024-05-23 15:32:54 +02:00
|
|
|
ioe: jmp save_image
|
2024-10-14 17:58:17 +02:00
|
|
|
|
2024-05-23 15:32:54 +02:00
|
|
|
iof: call load_image
|
|
|
|
xor %rax, %rax
|
|
|
|
xor %r13, %r13
|
|
|
|
jmp _execute
|
2024-10-14 17:58:17 +02:00
|
|
|
|
2024-05-23 15:32:54 +02:00
|
|
|
iog: mov $65536, %r13d
|
|
|
|
ret
|
2024-10-14 17:58:17 +02:00
|
|
|
|
2024-05-23 15:32:54 +02:00
|
|
|
ioh: add $8, %rbx
|
|
|
|
mov %eax, -4(%rbx)
|
|
|
|
lea astack-4(%rip), %rdx
|
|
|
|
neg %edx
|
|
|
|
lea 4*32-8(%rbx,%rdx), %rax
|
|
|
|
shr $2, %eax
|
|
|
|
mov %eax, (%rbx)
|
|
|
|
lea (%r12,%rdx), %rax
|
|
|
|
shr $2, %eax
|
|
|
|
ret
|
|
|
|
|
2024-10-28 15:34:47 +01:00
|
|
|
add_restrictions:
|
|
|
|
mov blocks(%rip), %rdi
|
|
|
|
lea mode_rw(%rip), %rsi
|
|
|
|
call unveil
|
2024-10-14 17:58:17 +02:00
|
|
|
|
2024-10-28 15:34:47 +01:00
|
|
|
mov rom(%rip), %rdi
|
|
|
|
lea mode_rw(%rip), %rsi
|
|
|
|
call unveil
|
|
|
|
|
|
|
|
xor %rdi, %rdi
|
|
|
|
xor %rsi, %rsi
|
|
|
|
call unveil
|
|
|
|
|
|
|
|
lea pledges(%rip), %rdi
|
|
|
|
xor %rsi, %rsi
|
|
|
|
call pledge
|
|
|
|
ret
|
|
|
|
|
|
|
|
main: xor %eax, %eax
|
2024-05-23 15:32:54 +02:00
|
|
|
lea memory(%rip), %r15
|
|
|
|
lea dstack-4(%rip), %rbx
|
|
|
|
lea astack-4(%rip), %r12
|
|
|
|
xor %r13d, %r13d
|
|
|
|
lea table(%rip), %rbp
|
|
|
|
mov 8(%rsp), %rcx
|
|
|
|
or %rcx, %rcx
|
|
|
|
jz 1f
|
|
|
|
mov 16(%rsp), %rcx
|
|
|
|
or %rcx, %rcx
|
|
|
|
jz 1f
|
|
|
|
mov %rcx, blocks(%rip)
|
|
|
|
mov 24(%rsp), %rcx
|
|
|
|
or %rcx, %rcx
|
|
|
|
jz 2f
|
|
|
|
mov %rcx, rom(%rip)
|
|
|
|
jmp 3f
|
|
|
|
1: lea default_blocks(%rip), %rcx
|
|
|
|
mov %rcx, blocks(%rip)
|
|
|
|
2: lea default_rom(%rip), %rcx
|
|
|
|
mov %rcx, rom(%rip)
|
2024-10-28 15:34:47 +01:00
|
|
|
3: call add_restrictions
|
|
|
|
call load_image
|
2024-05-23 15:32:54 +02:00
|
|
|
_execute:
|
|
|
|
jmp 3f
|
|
|
|
.align 64
|
|
|
|
2: mov (%r15,%r13,4), %r14d
|
|
|
|
movzbl %r14b, %edi
|
|
|
|
shr $8, %r14d
|
|
|
|
cmp $29, %edi
|
|
|
|
ja 1f
|
|
|
|
shl $5, %edi
|
|
|
|
add %rbp, %rdi
|
|
|
|
call *%rdi
|
|
|
|
1: movzbl %r14b, %edi
|
|
|
|
shr $8, %r14d
|
|
|
|
cmp $29, %edi
|
|
|
|
ja 1f
|
|
|
|
shl $5, %edi
|
|
|
|
add %rbp, %rdi
|
|
|
|
call *%rdi
|
|
|
|
1: movzbl %r14b, %edi
|
|
|
|
shr $8, %r14d
|
|
|
|
cmp $29, %edi
|
|
|
|
ja 1f
|
|
|
|
shl $5, %edi
|
|
|
|
add %rbp, %rdi
|
|
|
|
call *%rdi
|
|
|
|
1: mov %r14d, %edi
|
|
|
|
cmp $29, %edi
|
|
|
|
ja 1f
|
|
|
|
shl $5, %edi
|
|
|
|
add %rbp, %rdi
|
|
|
|
call *%rdi
|
|
|
|
1: inc %r13
|
|
|
|
3: cmp $65536, %r13
|
|
|
|
jl 2b
|
|
|
|
xor %edi, %edi
|
2024-10-14 17:58:17 +02:00
|
|
|
call exit /* exit program */
|