2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A#include <brand_misc.h>
2N/A
2N/A/*
2N/A * Each JMP must occupy 16 bytes
2N/A */
2N/A#define JMP \
2N/A pushl $_CONST(. - brand_handler_table); \
2N/A jmp brand_handler; \
2N/A .align 16;
2N/A
2N/A#define JMP4 JMP; JMP; JMP; JMP
2N/A#define JMP16 JMP4; JMP4; JMP4; JMP4
2N/A#define JMP64 JMP16; JMP16; JMP16; JMP16
2N/A#define JMP256 JMP64; JMP64; JMP64; JMP64
2N/A
2N/A#if defined(lint)
2N/A
2N/Avoid
2N/Abrand_handler_table(void)
2N/A{}
2N/A
2N/Avoid
2N/Abrand_handler(void)
2N/A{
2N/A}
2N/A
2N/A#else /* lint */
2N/A
2N/A /*
2N/A * On entry to this table, %eax will hold the return address. The
2N/A * location where we enter the table is a function of the system
2N/A * call number. The table needs the same alignment as the individual
2N/A * entries.
2N/A */
2N/A .align 16
2N/A ENTRY_NP(brand_handler_table)
2N/A JMP256
2N/A SET_SIZE(brand_handler_table)
2N/A
2N/A#define PIC_SETUP(r) \
2N/A call 9f; \
2N/A9: \
2N/A popl r; \
2N/A addl $_GLOBAL_OFFSET_TABLE_ + [. - 9b], r
2N/A
2N/A /*
2N/A * %eax - userland return address
2N/A * stack contains:
2N/A * | --------------------------------------
2N/A * v 4 | syscall arguments |
2N/A * %esp+0 | syscall number |
2N/A * --------------------------------------
2N/A */
2N/A ENTRY_NP(brand_handler)
2N/A pushl %ebp /* allocate a stack frame */
2N/A movl %esp, %ebp
2N/A
2N/A /* Save registers at the time of the syscall. */
2N/A movl $0, EH_LOCALS_GREG(TRAPNO)(%ebp)
2N/A movl $0, EH_LOCALS_GREG(ERR)(%ebp)
2N/A movl %ebx, EH_LOCALS_GREG(EBX)(%ebp)
2N/A movl %ecx, EH_LOCALS_GREG(ECX)(%ebp)
2N/A movl %edx, EH_LOCALS_GREG(EDX)(%ebp)
2N/A movl %edi, EH_LOCALS_GREG(EDI)(%ebp)
2N/A movl %esi, EH_LOCALS_GREG(ESI)(%ebp)
2N/A mov %cs, EH_LOCALS_GREG(CS)(%ebp)
2N/A mov %ds, EH_LOCALS_GREG(DS)(%ebp)
2N/A mov %es, EH_LOCALS_GREG(ES)(%ebp)
2N/A mov %fs, EH_LOCALS_GREG(FS)(%ebp)
2N/A mov %gs, EH_LOCALS_GREG(GS)(%ebp)
2N/A pushfl /* save syscall flags */
2N/A popl %ecx
2N/A movl %ecx, EH_LOCALS_GREG(EFL)(%ebp)
2N/A movl EH_ARGS_OFFSET(0)(%ebp), %ecx /* save syscall ebp */
2N/A movl %ecx, EH_LOCALS_GREG(EBP)(%ebp)
2N/A movl %ebp, %ecx /* save syscall esp */
2N/A addl $CPTRSIZE, %ecx
2N/A movl %ecx, EH_LOCALS_GREG(ESP)(%ebp)
2N/A
2N/A /*
2N/A * The kernel drops us into the middle of the brand_handle_table
2N/A * above that then pushes that table offset onto the stack, and calls
2N/A * into brand_handler. That offset indicates the system call number
2N/A * while %eax holds the return address for the system call. We replace
2N/A * the value on the stack with the return address, and use the value to
2N/A * compute the system call number by dividing by the table entry size.
2N/A */
2N/A xchgl CPTRSIZE(%ebp), %eax /* swap JMP table offset and ret addr */
2N/A shrl $4, %eax /* table_offset/size = syscall num */
2N/A movl %eax, EH_LOCALS_GREG(EAX)(%ebp) /* save syscall num */
2N/A
2N/A /*
2N/A * Finish setting up our stack frame. We would normally do this
2N/A * upon entry to this function, but in this case we delayed it
2N/A * because a "sub" operation can modify flags and we wanted to
2N/A * save the flags into the gregset_t above before they get modified.
2N/A *
2N/A * Our stack frame format is documented in brand_misc.h.
2N/A */
2N/A subl $EH_LOCALS_SIZE, %esp
2N/A
2N/A /* Look up the system call's entry in the sysent table */
2N/A PIC_SETUP(%ecx)
2N/A movl brand_sysent_table@GOT(%ecx), %edx /* %edx = sysent_table */
2N/A shll $3, %eax /* each entry is 8 bytes */
2N/A add %eax, %edx /* %edx = sysent entry address */
2N/A
2N/A /*
2N/A * Get the return value flag and the number of arguments from the
2N/A * sysent table.
2N/A */
2N/A movl CPTRSIZE(%edx), %ecx /* number of args + rv flag */
2N/A andl $RV_MASK, %ecx /* strip out number of args */
2N/A movl %ecx, EH_LOCALS_RVFLAG(%ebp) /* save rv flag */
2N/A movl CPTRSIZE(%edx), %ecx /* number of args + rv flag */
2N/A andl $NARGS_MASK, %ecx /* strip out rv flag */
2N/A
2N/A /*
2N/A * Setup arguments for our emulation call. Our input arguments,
2N/A * 0 to N, will become emulation call arguments 1 to N+1.
2N/A * %ecx == number of arguments.
2N/A */
2N/A movl %ebp, %esi /* args are at 12(%ebp) */
2N/A addl $EH_ARGS_OFFSET(3), %esi
2N/A movl %esp, %edi /* copy args to 4(%esp) */
2N/A addl $EH_ARGS_OFFSET(1), %edi
2N/A rep; smovl /* copy: (%esi) -> (%edi) */
2N/A /* copy: %ecx 32-bit words */
2N/A movl EH_LOCALS_GREG(ESI)(%ebp), %esi /* restore %esi */
2N/A movl EH_LOCALS_GREG(EDI)(%ebp), %edi /* restore %edi */
2N/A
2N/A /*
2N/A * The first parameter to the emulation callback function is a
2N/A * pointer to a sysret_t structure.
2N/A */
2N/A movl %ebp, %ecx
2N/A addl $EH_LOCALS_SYSRET, %ecx
2N/A movl %ecx, EH_ARGS_OFFSET(0)(%esp) /* arg0 == sysret_t ptr */
2N/A
2N/A /* invoke the emulation routine */
2N/A ALTENTRY(brand_handler_savepc)
2N/A call *(%edx) /* call emulation routine */
2N/A
2N/A /* restore scratch registers */
2N/A movl EH_LOCALS_GREG(ECX)(%ebp), %ecx /* restore %ecx */
2N/A movl EH_LOCALS_GREG(EDX)(%ebp), %edx /* restore %edx */
2N/A
2N/A /* Check for syscall emulation success or failure */
2N/A cmpl $0, %eax /* check for an error */
2N/A je success
2N/A stc /* failure, set carry flag */
2N/A jmp return /* return, %rax == errno */
2N/A
2N/Asuccess:
2N/A /* There is always at least one return value. */
2N/A movl EH_LOCALS_SYSRET1(%ebp), %eax /* %eax == sys_rval1 */
2N/A cmpl $RV_DEFAULT, EH_LOCALS_RVFLAG(%ebp) /* check rv flag */
2N/A je clear_carry
2N/A mov EH_LOCALS_SYSRET2(%ebp), %edx /* %edx == sys_rval2 */
2N/Aclear_carry:
2N/A clc /* success, clear carry flag */
2N/A
2N/Areturn:
2N/A movl %ebp, %esp /* restore stack */
2N/A popl %ebp
2N/A ret /* ret to instr after syscall */
2N/A SET_SIZE(brand_handler)
2N/A
2N/A
2N/A#endif /* lint */