162306a36Sopenharmony_ci/* SPDX-License-Identifier: LGPL-2.1 OR MIT */ 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * i386 specific definitions for NOLIBC 462306a36Sopenharmony_ci * Copyright (C) 2017-2022 Willy Tarreau <w@1wt.eu> 562306a36Sopenharmony_ci */ 662306a36Sopenharmony_ci 762306a36Sopenharmony_ci#ifndef _NOLIBC_ARCH_I386_H 862306a36Sopenharmony_ci#define _NOLIBC_ARCH_I386_H 962306a36Sopenharmony_ci 1062306a36Sopenharmony_ci#include "compiler.h" 1162306a36Sopenharmony_ci#include "crt.h" 1262306a36Sopenharmony_ci 1362306a36Sopenharmony_ci/* Syscalls for i386 : 1462306a36Sopenharmony_ci * - mostly similar to x86_64 1562306a36Sopenharmony_ci * - registers are 32-bit 1662306a36Sopenharmony_ci * - syscall number is passed in eax 1762306a36Sopenharmony_ci * - arguments are in ebx, ecx, edx, esi, edi, ebp respectively 1862306a36Sopenharmony_ci * - all registers are preserved (except eax of course) 1962306a36Sopenharmony_ci * - the system call is performed by calling int $0x80 2062306a36Sopenharmony_ci * - syscall return comes in eax 2162306a36Sopenharmony_ci * - the arguments are cast to long and assigned into the target registers 2262306a36Sopenharmony_ci * which are then simply passed as registers to the asm code, so that we 2362306a36Sopenharmony_ci * don't have to experience issues with register constraints. 2462306a36Sopenharmony_ci * - the syscall number is always specified last in order to allow to force 2562306a36Sopenharmony_ci * some registers before (gcc refuses a %-register at the last position). 2662306a36Sopenharmony_ci * 2762306a36Sopenharmony_ci * Also, i386 supports the old_select syscall if newselect is not available 2862306a36Sopenharmony_ci */ 2962306a36Sopenharmony_ci#define __ARCH_WANT_SYS_OLD_SELECT 3062306a36Sopenharmony_ci 3162306a36Sopenharmony_ci#define my_syscall0(num) \ 3262306a36Sopenharmony_ci({ \ 3362306a36Sopenharmony_ci long _ret; \ 3462306a36Sopenharmony_ci register long _num __asm__ ("eax") = (num); \ 3562306a36Sopenharmony_ci \ 3662306a36Sopenharmony_ci __asm__ volatile ( \ 3762306a36Sopenharmony_ci "int $0x80\n" \ 3862306a36Sopenharmony_ci : "=a" (_ret) \ 3962306a36Sopenharmony_ci : "0"(_num) \ 4062306a36Sopenharmony_ci : "memory", "cc" \ 4162306a36Sopenharmony_ci ); \ 4262306a36Sopenharmony_ci _ret; \ 4362306a36Sopenharmony_ci}) 4462306a36Sopenharmony_ci 4562306a36Sopenharmony_ci#define my_syscall1(num, arg1) \ 4662306a36Sopenharmony_ci({ \ 4762306a36Sopenharmony_ci long _ret; \ 4862306a36Sopenharmony_ci register long _num __asm__ ("eax") = (num); \ 4962306a36Sopenharmony_ci register long _arg1 __asm__ ("ebx") = (long)(arg1); \ 5062306a36Sopenharmony_ci \ 5162306a36Sopenharmony_ci __asm__ volatile ( \ 5262306a36Sopenharmony_ci "int $0x80\n" \ 5362306a36Sopenharmony_ci : "=a" (_ret) \ 5462306a36Sopenharmony_ci : "r"(_arg1), \ 5562306a36Sopenharmony_ci "0"(_num) \ 5662306a36Sopenharmony_ci : "memory", "cc" \ 5762306a36Sopenharmony_ci ); \ 5862306a36Sopenharmony_ci _ret; \ 5962306a36Sopenharmony_ci}) 6062306a36Sopenharmony_ci 6162306a36Sopenharmony_ci#define my_syscall2(num, arg1, arg2) \ 6262306a36Sopenharmony_ci({ \ 6362306a36Sopenharmony_ci long _ret; \ 6462306a36Sopenharmony_ci register long _num __asm__ ("eax") = (num); \ 6562306a36Sopenharmony_ci register long _arg1 __asm__ ("ebx") = (long)(arg1); \ 6662306a36Sopenharmony_ci register long _arg2 __asm__ ("ecx") = (long)(arg2); \ 6762306a36Sopenharmony_ci \ 6862306a36Sopenharmony_ci __asm__ volatile ( \ 6962306a36Sopenharmony_ci "int $0x80\n" \ 7062306a36Sopenharmony_ci : "=a" (_ret) \ 7162306a36Sopenharmony_ci : "r"(_arg1), "r"(_arg2), \ 7262306a36Sopenharmony_ci "0"(_num) \ 7362306a36Sopenharmony_ci : "memory", "cc" \ 7462306a36Sopenharmony_ci ); \ 7562306a36Sopenharmony_ci _ret; \ 7662306a36Sopenharmony_ci}) 7762306a36Sopenharmony_ci 7862306a36Sopenharmony_ci#define my_syscall3(num, arg1, arg2, arg3) \ 7962306a36Sopenharmony_ci({ \ 8062306a36Sopenharmony_ci long _ret; \ 8162306a36Sopenharmony_ci register long _num __asm__ ("eax") = (num); \ 8262306a36Sopenharmony_ci register long _arg1 __asm__ ("ebx") = (long)(arg1); \ 8362306a36Sopenharmony_ci register long _arg2 __asm__ ("ecx") = (long)(arg2); \ 8462306a36Sopenharmony_ci register long _arg3 __asm__ ("edx") = (long)(arg3); \ 8562306a36Sopenharmony_ci \ 8662306a36Sopenharmony_ci __asm__ volatile ( \ 8762306a36Sopenharmony_ci "int $0x80\n" \ 8862306a36Sopenharmony_ci : "=a" (_ret) \ 8962306a36Sopenharmony_ci : "r"(_arg1), "r"(_arg2), "r"(_arg3), \ 9062306a36Sopenharmony_ci "0"(_num) \ 9162306a36Sopenharmony_ci : "memory", "cc" \ 9262306a36Sopenharmony_ci ); \ 9362306a36Sopenharmony_ci _ret; \ 9462306a36Sopenharmony_ci}) 9562306a36Sopenharmony_ci 9662306a36Sopenharmony_ci#define my_syscall4(num, arg1, arg2, arg3, arg4) \ 9762306a36Sopenharmony_ci({ \ 9862306a36Sopenharmony_ci long _ret; \ 9962306a36Sopenharmony_ci register long _num __asm__ ("eax") = (num); \ 10062306a36Sopenharmony_ci register long _arg1 __asm__ ("ebx") = (long)(arg1); \ 10162306a36Sopenharmony_ci register long _arg2 __asm__ ("ecx") = (long)(arg2); \ 10262306a36Sopenharmony_ci register long _arg3 __asm__ ("edx") = (long)(arg3); \ 10362306a36Sopenharmony_ci register long _arg4 __asm__ ("esi") = (long)(arg4); \ 10462306a36Sopenharmony_ci \ 10562306a36Sopenharmony_ci __asm__ volatile ( \ 10662306a36Sopenharmony_ci "int $0x80\n" \ 10762306a36Sopenharmony_ci : "=a" (_ret) \ 10862306a36Sopenharmony_ci : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), \ 10962306a36Sopenharmony_ci "0"(_num) \ 11062306a36Sopenharmony_ci : "memory", "cc" \ 11162306a36Sopenharmony_ci ); \ 11262306a36Sopenharmony_ci _ret; \ 11362306a36Sopenharmony_ci}) 11462306a36Sopenharmony_ci 11562306a36Sopenharmony_ci#define my_syscall5(num, arg1, arg2, arg3, arg4, arg5) \ 11662306a36Sopenharmony_ci({ \ 11762306a36Sopenharmony_ci long _ret; \ 11862306a36Sopenharmony_ci register long _num __asm__ ("eax") = (num); \ 11962306a36Sopenharmony_ci register long _arg1 __asm__ ("ebx") = (long)(arg1); \ 12062306a36Sopenharmony_ci register long _arg2 __asm__ ("ecx") = (long)(arg2); \ 12162306a36Sopenharmony_ci register long _arg3 __asm__ ("edx") = (long)(arg3); \ 12262306a36Sopenharmony_ci register long _arg4 __asm__ ("esi") = (long)(arg4); \ 12362306a36Sopenharmony_ci register long _arg5 __asm__ ("edi") = (long)(arg5); \ 12462306a36Sopenharmony_ci \ 12562306a36Sopenharmony_ci __asm__ volatile ( \ 12662306a36Sopenharmony_ci "int $0x80\n" \ 12762306a36Sopenharmony_ci : "=a" (_ret) \ 12862306a36Sopenharmony_ci : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), "r"(_arg5), \ 12962306a36Sopenharmony_ci "0"(_num) \ 13062306a36Sopenharmony_ci : "memory", "cc" \ 13162306a36Sopenharmony_ci ); \ 13262306a36Sopenharmony_ci _ret; \ 13362306a36Sopenharmony_ci}) 13462306a36Sopenharmony_ci 13562306a36Sopenharmony_ci#define my_syscall6(num, arg1, arg2, arg3, arg4, arg5, arg6) \ 13662306a36Sopenharmony_ci({ \ 13762306a36Sopenharmony_ci long _eax = (long)(num); \ 13862306a36Sopenharmony_ci long _arg6 = (long)(arg6); /* Always in memory */ \ 13962306a36Sopenharmony_ci __asm__ volatile ( \ 14062306a36Sopenharmony_ci "pushl %[_arg6]\n\t" \ 14162306a36Sopenharmony_ci "pushl %%ebp\n\t" \ 14262306a36Sopenharmony_ci "movl 4(%%esp),%%ebp\n\t" \ 14362306a36Sopenharmony_ci "int $0x80\n\t" \ 14462306a36Sopenharmony_ci "popl %%ebp\n\t" \ 14562306a36Sopenharmony_ci "addl $4,%%esp\n\t" \ 14662306a36Sopenharmony_ci : "+a"(_eax) /* %eax */ \ 14762306a36Sopenharmony_ci : "b"(arg1), /* %ebx */ \ 14862306a36Sopenharmony_ci "c"(arg2), /* %ecx */ \ 14962306a36Sopenharmony_ci "d"(arg3), /* %edx */ \ 15062306a36Sopenharmony_ci "S"(arg4), /* %esi */ \ 15162306a36Sopenharmony_ci "D"(arg5), /* %edi */ \ 15262306a36Sopenharmony_ci [_arg6]"m"(_arg6) /* memory */ \ 15362306a36Sopenharmony_ci : "memory", "cc" \ 15462306a36Sopenharmony_ci ); \ 15562306a36Sopenharmony_ci _eax; \ 15662306a36Sopenharmony_ci}) 15762306a36Sopenharmony_ci 15862306a36Sopenharmony_ci/* startup code */ 15962306a36Sopenharmony_ci/* 16062306a36Sopenharmony_ci * i386 System V ABI mandates: 16162306a36Sopenharmony_ci * 1) last pushed argument must be 16-byte aligned. 16262306a36Sopenharmony_ci * 2) The deepest stack frame should be set to zero 16362306a36Sopenharmony_ci * 16462306a36Sopenharmony_ci */ 16562306a36Sopenharmony_civoid __attribute__((weak, noreturn, optimize("Os", "omit-frame-pointer"))) __no_stack_protector _start(void) 16662306a36Sopenharmony_ci{ 16762306a36Sopenharmony_ci __asm__ volatile ( 16862306a36Sopenharmony_ci "xor %ebp, %ebp\n" /* zero the stack frame */ 16962306a36Sopenharmony_ci "mov %esp, %eax\n" /* save stack pointer to %eax, as arg1 of _start_c */ 17062306a36Sopenharmony_ci "add $12, %esp\n" /* avoid over-estimating after the 'and' & 'sub' below */ 17162306a36Sopenharmony_ci "and $-16, %esp\n" /* the %esp must be 16-byte aligned on 'call' */ 17262306a36Sopenharmony_ci "sub $12, %esp\n" /* sub 12 to keep it aligned after the push %eax */ 17362306a36Sopenharmony_ci "push %eax\n" /* push arg1 on stack to support plain stack modes too */ 17462306a36Sopenharmony_ci "call _start_c\n" /* transfer to c runtime */ 17562306a36Sopenharmony_ci "hlt\n" /* ensure it does not return */ 17662306a36Sopenharmony_ci ); 17762306a36Sopenharmony_ci __builtin_unreachable(); 17862306a36Sopenharmony_ci} 17962306a36Sopenharmony_ci 18062306a36Sopenharmony_ci#endif /* _NOLIBC_ARCH_I386_H */ 181