iPXE
com32_call.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008 Daniel Verkamp <daniel@drv.nu>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  */
19 
20 /**
21  * @file SYSLINUX COM32 helpers
22  *
23  */
24 
25 FILE_LICENCE ( GPL2_OR_LATER );
26 
27 #include <stdint.h>
28 #include <realmode.h>
29 #include <comboot.h>
30 #include <assert.h>
31 #include <ipxe/uaccess.h>
32 
33 static com32sys_t __bss16 ( com32_regs );
34 #define com32_regs __use_data16 ( com32_regs )
35 
37 #define com32_int_vector __use_data16 ( com32_int_vector )
38 
40 #define com32_farcall_proc __use_data16 ( com32_farcall_proc )
41 
42 uint16_t __bss16 ( com32_saved_sp );
43 
44 /**
45  * Interrupt call helper
46  */
47 void __asmcall com32_intcall ( uint8_t interrupt, physaddr_t inregs_phys, physaddr_t outregs_phys ) {
48 
49  DBGC ( &com32_regs, "COM32 INT%x in %#08lx out %#08lx\n",
50  interrupt, inregs_phys, outregs_phys );
51 
53  phys_to_user ( inregs_phys ), 0,
54  sizeof(com32sys_t) );
55 
57 
59  REAL_CODE ( /* Save all registers */
60  "pushal\n\t"
61  "pushw %%ds\n\t"
62  "pushw %%es\n\t"
63  "pushw %%fs\n\t"
64  "pushw %%gs\n\t"
65  /* Mask off unsafe flags */
66  "movl (com32_regs + 40), %%eax\n\t"
67  "andl $0x200cd7, %%eax\n\t"
68  "movl %%eax, (com32_regs + 40)\n\t"
69  /* Load com32_regs into the actual registers */
70  "movw %%sp, %%ss:(com32_saved_sp)\n\t"
71  "movw $com32_regs, %%sp\n\t"
72  "popw %%gs\n\t"
73  "popw %%fs\n\t"
74  "popw %%es\n\t"
75  "popw %%ds\n\t"
76  "popal\n\t"
77  "popfl\n\t"
78  "movw %%ss:(com32_saved_sp), %%sp\n\t"
79  /* patch INT instruction */
80  "pushw %%ax\n\t"
81  "movb %%ss:(com32_int_vector), %%al\n\t"
82  "movb %%al, %%cs:(com32_intcall_instr + 1)\n\t"
83  /* perform a jump to avoid problems with cache
84  * consistency in self-modifying code on some CPUs (486)
85  */
86  "jmp 1f\n"
87  "1:\n\t"
88  "popw %%ax\n\t"
89  "com32_intcall_instr:\n\t"
90  /* INT instruction to be patched */
91  "int $0xFF\n\t"
92  /* Copy regs back to com32_regs */
93  "movw %%sp, %%ss:(com32_saved_sp)\n\t"
94  "movw $(com32_regs + 44), %%sp\n\t"
95  "pushfl\n\t"
96  "pushal\n\t"
97  "pushw %%ds\n\t"
98  "pushw %%es\n\t"
99  "pushw %%fs\n\t"
100  "pushw %%gs\n\t"
101  "movw %%ss:(com32_saved_sp), %%sp\n\t"
102  /* Restore registers */
103  "popw %%gs\n\t"
104  "popw %%fs\n\t"
105  "popw %%es\n\t"
106  "popw %%ds\n\t"
107  "popal\n\t")
108  : : );
109 
110  if ( outregs_phys ) {
111  memcpy_user ( phys_to_user ( outregs_phys ), 0,
112  virt_to_user( &com32_regs ), 0,
113  sizeof(com32sys_t) );
114  }
115 }
116 
117 /**
118  * Farcall helper
119  */
120 void __asmcall com32_farcall ( uint32_t proc, physaddr_t inregs_phys, physaddr_t outregs_phys ) {
121 
122  DBGC ( &com32_regs, "COM32 farcall %04x:%04x in %#08lx out %#08lx\n",
123  ( proc >> 16 ), ( proc & 0xffff ), inregs_phys, outregs_phys );
124 
126  phys_to_user ( inregs_phys ), 0,
127  sizeof(com32sys_t) );
128 
129  com32_farcall_proc = proc;
130 
132  REAL_CODE ( /* Save all registers */
133  "pushal\n\t"
134  "pushw %%ds\n\t"
135  "pushw %%es\n\t"
136  "pushw %%fs\n\t"
137  "pushw %%gs\n\t"
138  /* Mask off unsafe flags */
139  "movl (com32_regs + 40), %%eax\n\t"
140  "andl $0x200cd7, %%eax\n\t"
141  "movl %%eax, (com32_regs + 40)\n\t"
142  /* Load com32_regs into the actual registers */
143  "movw %%sp, %%ss:(com32_saved_sp)\n\t"
144  "movw $com32_regs, %%sp\n\t"
145  "popw %%gs\n\t"
146  "popw %%fs\n\t"
147  "popw %%es\n\t"
148  "popw %%ds\n\t"
149  "popal\n\t"
150  "popfl\n\t"
151  "movw %%ss:(com32_saved_sp), %%sp\n\t"
152  /* Call procedure */
153  "lcall *%%ss:(com32_farcall_proc)\n\t"
154  /* Copy regs back to com32_regs */
155  "movw %%sp, %%ss:(com32_saved_sp)\n\t"
156  "movw $(com32_regs + 44), %%sp\n\t"
157  "pushfl\n\t"
158  "pushal\n\t"
159  "pushw %%ds\n\t"
160  "pushw %%es\n\t"
161  "pushw %%fs\n\t"
162  "pushw %%gs\n\t"
163  "movw %%ss:(com32_saved_sp), %%sp\n\t"
164  /* Restore registers */
165  "popw %%gs\n\t"
166  "popw %%fs\n\t"
167  "popw %%es\n\t"
168  "popw %%ds\n\t"
169  "popal\n\t")
170  : : );
171 
172  if ( outregs_phys ) {
173  memcpy_user ( phys_to_user ( outregs_phys ), 0,
174  virt_to_user( &com32_regs ), 0,
175  sizeof(com32sys_t) );
176  }
177 }
178 
179 /**
180  * CDECL farcall helper
181  */
182 int __asmcall com32_cfarcall ( uint32_t proc, physaddr_t stack, size_t stacksz ) {
183  int32_t eax;
184 
185  DBGC ( &com32_regs, "COM32 cfarcall %04x:%04x params %#08lx+%#zx\n",
186  ( proc >> 16 ), ( proc & 0xffff ), stack, stacksz );
187 
188  copy_user_to_rm_stack ( phys_to_user ( stack ), stacksz );
189  com32_farcall_proc = proc;
190 
192  REAL_CODE ( "lcall *%%ss:(com32_farcall_proc)\n\t" )
193  : "=a" (eax)
194  :
195  : "ecx", "edx" );
196 
197  remove_user_from_rm_stack ( 0, stacksz );
198 
199  return eax;
200 }
unsigned short uint16_t
Definition: stdint.h:11
void __asmcall com32_farcall(uint32_t proc, physaddr_t inregs_phys, physaddr_t outregs_phys)
Farcall helper.
Definition: com32_call.c:120
static com32sys_t __bss16(com32_regs)
#define com32_regs
Definition: com32_call.c:34
#define DBGC(...)
Definition: compiler.h:505
userptr_t phys_to_user(unsigned long phys_addr)
Convert physical address to user pointer.
SYSLINUX COMBOOT.
int __asmcall com32_cfarcall(uint32_t proc, physaddr_t stack, size_t stacksz)
CDECL farcall helper.
Definition: com32_call.c:182
Access to external ("user") memory.
uint16_t copy_user_to_rm_stack(userptr_t data, size_t size)
Allocate space on the real-mode stack and copy data there from a user buffer.
Definition: librm_mgmt.c:64
#define __asmcall
Declare a function with standard calling conventions.
Definition: compiler.h:12
Assertions.
#define com32_farcall_proc
Definition: com32_call.c:40
uint32_t eax
Definition: string.h:234
unsigned char uint8_t
Definition: stdint.h:10
unsigned int uint32_t
Definition: stdint.h:12
FILE_LICENCE(GPL2_OR_LATER)
__asm__ __volatile__("\n1:\n\t" "movb -1(%2,%1), %%al\n\t" "stosb\n\t" "loop 1b\n\t" "xorl %%eax, %%eax\n\t" "mov %3, %1\n\t" "rep stosb\n\t" :"=&D"(discard_D), "=&c"(discard_c) :"r"(data), "g"(pad_len), "0"(value0), "1"(len) :"eax")
unsigned long physaddr_t
Definition: stdint.h:20
__asm__(".section \".rodata\", \"a\", " PROGBITS "\n\t" "\nprivate_key_data:\n\t" ".size private_key_data, ( . - private_key_data )\n\t" ".equ private_key_len, ( . - private_key_data )\n\t" ".previous\n\t")
signed int int32_t
Definition: stdint.h:17
userptr_t virt_to_user(volatile const void *addr)
Convert virtual address to user pointer.
void remove_user_from_rm_stack(userptr_t data, size_t size)
Deallocate space on the real-mode stack, optionally copying back data to a user buffer.
Definition: librm_mgmt.c:79
#define REAL_CODE(asm_code_str)
Definition: libkir.h:226
#define com32_int_vector
Definition: com32_call.c:37
void interrupt(int intr, struct interrupt_frame32 *frame32, struct interrupt_frame64 *frame64)
Interrupt handler.
Definition: librm_mgmt.c:246
void memcpy_user(userptr_t dest, off_t dest_off, userptr_t src, off_t src_off, size_t len)
Copy data between user buffers.
void __asmcall com32_intcall(uint8_t interrupt, physaddr_t inregs_phys, physaddr_t outregs_phys)
Interrupt call helper.
Definition: com32_call.c:47