Simplify 32-bit x86 assembly

This commit is contained in:
Jeremy Soller
2022-08-19 10:13:28 -06:00
parent 00fbd71519
commit af040956df
2 changed files with 53 additions and 42 deletions
-42
View File
@@ -1,42 +0,0 @@
BITS 32
section .text
global ustart
extern start
ustart:
; Setup a stack.
mov eax, 0x21000384 ; SYS_CLASS_FILE+SYS_ARG_SLICE+SYS_MMAP
mov ebx, 0xFFFFFFFF ; dummy `fd`, indicates anonymous map
mov ecx, map ; pointer to Map struct
mov edx, map_size ; size of Map struct
int 0x80
; Test for success (nonzero value).
cmp eax, 0
jg .continue
; (failure)
ud2
.continue:
; Subtract 16 since all instructions seem to hate non-canonical ESP values :)
lea esp, [eax+size-16]
mov ebp, esp
; Stack has the same alignment as `size`.
call start
; `start` must never return.
ud2
section .rodata
map:
dd 0 ; offset (unused)
dd size
dd flags
dd address - size
map_size equ $ - map
size equ 65536 ; 64 KiB
address equ 0x80000000 ; highest possible (normal) user address, why not
flags equ 0x0006000E ; PROT_READ+PROT_WRITE, MAP_PRIVATE+MAP_FIXED_NOREPLACE
+53
View File
@@ -0,0 +1,53 @@
use core::mem;
use syscall::{
data::Map,
flag::MapFlags,
number::SYS_FMAP,
};
const STACK_SIZE: usize = 64 * 1024; // 64 KiB
static MAP: Map = Map {
offset: 0,
size: STACK_SIZE,
flags: MapFlags::PROT_READ
.union(MapFlags::PROT_WRITE)
.union(MapFlags::MAP_PRIVATE)
.union(MapFlags::MAP_FIXED_NOREPLACE),
address: 0x8000_0000 - STACK_SIZE, // highest possible user address
};
#[naked]
#[no_mangle]
pub unsafe extern "C" fn ustart() {
core::arch::asm!(
"
# Setup a stack.
mov eax, {number}
mov ebx, {fd}
lea ecx, {map} # pointer to Map struct
mov edx, {map_size} # size of Map struct
int 0x80
# Test for success (nonzero value).
cmp eax, 0
jg 1f
# (failure)
ud2
1:
# Subtract 16 since all instructions seem to hate non-canonical ESP values :)
lea esp, [eax+{stack_size}-16]
mov ebp, esp
# Stack has the same alignment as `size`.
call start
# `start` must never return.
ud2
",
fd = const usize::MAX, // dummy fd indicates anonymous map
map = sym MAP,
map_size = const mem::size_of::<Map>(),
number = const SYS_FMAP,
stack_size = const STACK_SIZE,
options(noreturn),
);
}