1 ;; -----------------------------------------------------------------------
3 ;; Copyright 1994-2008 H. Peter Anvin - All Rights Reserved
5 ;; This program is free software; you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation, Inc., 53 Temple Place Ste 330,
8 ;; Boston MA 02111-1307, USA; either version 2 of the License, or
9 ;; (at your option) any later version; incorporated herein by reference.
11 ;; -----------------------------------------------------------------------
16 ;; Common code for running a COM32 image
20 ; Load a COM32 image. A COM32 image is the 32-bit analogue to a DOS
21 ; .com file. A COM32 image is loaded at address 0x101000, with %esp
22 ; set to the high end of usable memory.
24 ; A COM32 image should begin with the magic bytes:
25 ; B8 FF 4C CD 21, which is "mov eax,0x21cd4cff" in 32-bit mode and
26 ; "mov ax,0x4cff; int 0x21" in 16-bit mode. This will abort the
27 ; program with an error if run in 16-bit mode.
30 pm_entry: equ 0x101000
45 push si ; Save file handle
46 push dx ; File length held in DX:AX
49 call make_plain_cmdline
50 ; Copy the command line into the low cmdline buffer
56 inc cx ; Include final null
60 call highmemsize ; We need the high memory size...
61 call comboot_setup_api ; Set up the COMBOOT-style API
63 mov edi,pm_entry ; Load address
66 xor dx,dx ; No padding
67 mov bx,abort_check ; Don't print dots, but allow abort
71 mov ebx,com32_call_start ; Where to go in PM
78 mov [RealModeSSSP+2],ss
85 mov byte [bcopy_gdt.TSS+5],89h ; Mark TSS unbusy
87 lgdt [bcopy_gdt] ; We can use the same GDT just fine
88 lidt [com32_pmidt] ; Set up the IDT
91 mov cr0,eax ; Enter protected mode
96 xor eax,eax ; Available for future use...
101 mov al,PM_DS32 ; Set up data segments
106 mov al,PM_TSS ; Be nice to Intel's VT by
107 ltr ax ; giving it a valid TR
109 mov esp,[PMESP] ; Load protmode %esp if available
110 jmp ebx ; Go to where we need to go
113 ; This is invoked right before the actually starting the COM32
114 ; progam, in 32-bit mode...
118 ; Point the stack to the end of high memory
120 mov esp,[word HighMemSize]
123 ; Set up the protmode IDT and the interrupt jump buffers
124 ; We set these up in the system area at 0x100000,
125 ; but we could also put them beyond the stack.
129 ; Form an interrupt gate descriptor
130 mov eax,0x00200000+((pm_idt+8*256)&0x0000ffff)
131 mov ebx,0x0000ee00+((pm_idt+8*256)&0xffff0000)
146 ; Each entry in the interrupt jump buffer contains
147 ; the following instructions:
150 ; 00000001 B0xx mov al,<interrupt#>
151 ; 00000003 E9xxxxxxxx jmp com32_handle_interrupt
154 mov ebx,com32_handle_interrupt-(pm_idt+8*256+8)
158 sub [edi-2],cl ; Interrupt #
165 ; Now everything is set up for interrupts...
167 push dword com32_cfarcall ; Cfarcall entry point
168 push dword com32_farcall ; Farcall entry point
169 push dword (1 << 16) ; 64K bounce buffer
170 push dword (comboot_seg << 4) ; Bounce buffer address
171 push dword com32_intcall ; Intcall entry point
172 push dword command_line ; Command line pointer
173 push dword 6 ; Argument count
174 sti ; Interrupts OK now
175 call pm_entry ; Run the program...
176 ; ... on return, fall through to com32_exit ...
179 mov bx,com32_done ; Return to command loop
184 mov [PMESP],esp ; Save exit %esp
185 xor esp,esp ; Make sure the high bits are zero
186 jmp PM_CS16:.in_pm16 ; Return to 16-bit mode first
190 mov ax,PM_DS16_RM ; Real-mode-like segment
197 lidt [com32_rmidt] ; Real-mode IDT (rm needs no GDT)
203 .in_rm: ; Back in real mode
204 mov ax,cs ; Set up sane segments
209 lss sp,[RealModeSSSP] ; Restore stack
210 jmp bx ; Go to whereever we need to go...
218 ; 16-bit support code
223 ; 16-bit interrupt-handling code
226 pushf ; Flags on stack
227 push cs ; Return segment
228 push word .cont ; Return address
229 push dword edx ; Segment:offset of IVT entry
230 retf ; Invoke IVT routine
231 .cont: ; ... on resume ...
232 mov ebx,com32_int_resume
233 jmp com32_enter_pm ; Go back to PM
236 ; 16-bit intcall/farcall handling code
245 mov [cs:Com32SysSP],sp
246 retf ; Invoke routine
248 ; We clean up SP here because we don't know if the
249 ; routine returned with RET, RETF or IRET
250 mov sp,[cs:Com32SysSP]
257 mov ebx,com32_syscall.resume
261 ; 16-bit cfarcall handing code
266 mov sp,[cs:Com32SysSP]
267 mov [cs:RealModeEAX],eax
268 mov ebx,com32_cfarcall.resume
272 ; 32-bit support code
277 ; This is invoked on getting an interrupt in protected mode. At
278 ; this point, we need to context-switch to real mode and invoke
279 ; the interrupt routine.
281 ; When this gets invoked, the registers are saved on the stack and
282 ; AL contains the register number.
284 com32_handle_interrupt:
286 xor ebx,ebx ; Actually makes the code smaller
287 mov edx,[ebx+eax*4] ; Get the segment:offset of the routine
289 jmp com32_enter_rm ; Go to real mode
296 ; Intcall/farcall invocation. We manifest a structure on the real-mode stack,
297 ; containing the com32sys_t structure from <com32.h> as well as
298 ; the following entries (from low to high address):
302 ; - Return segment (== real mode cs == 0)
306 pushfd ; Save IF among other things...
307 pushad ; We only need to save some, but...
309 mov eax,[esp+10*4] ; CS:IP
314 pushfd ; Save IF among other things...
315 pushad ; We only need to save some, but...
317 movzx eax,byte [esp+10*4] ; INT number
318 mov eax,[eax*4] ; Get CS:IP from low memory
323 movzx edi,word [word RealModeSSSP]
324 movzx ebx,word [word RealModeSSSP+2]
325 sub edi,54 ; Allocate 54 bytes
326 mov [word RealModeSSSP],di
328 add edi,ebx ; Create linear address
330 mov esi,[esp+11*4] ; Source regs
332 mov cl,11 ; 44 bytes to copy
335 ; EAX is already set up to be CS:IP
336 stosd ; Save in stack frame
337 mov eax,com32_sys_rm.return ; Return seg:offs
338 stosd ; Save in stack frame
339 mov eax,[edi-12] ; Return flags
340 and eax,0x200cd7 ; Mask (potentially) unsafe flags
341 mov [edi-12],eax ; Primary flags entry
345 jmp com32_enter_rm ; Go to real mode
347 ; On return, the 44-byte return structure is on the
348 ; real-mode stack, plus the 10 additional bytes used
349 ; by the target address (see above.)
351 movzx esi,word [word RealModeSSSP]
352 movzx eax,word [word RealModeSSSP+2]
353 mov edi,[esp+12*4] ; Dest regs
355 add esi,eax ; Create linear address
356 and edi,edi ; NULL pointer?
358 .no_copy: mov edi,esi ; Do a dummy copy-to-self
359 .do_copy: xor ecx,ecx
361 rep movsd ; Copy register block
363 add dword [word RealModeSSSP],54 ; Remove from stack
367 ret ; Return to 32-bit program
370 ; Cfarcall invocation. We copy the stack frame to the real-mode stack,
371 ; followed by the return CS:IP and the CS:IP of the target function.
378 mov ecx,[esp+12*4] ; Size of stack frame
380 movzx edi,word [word RealModeSSSP]
381 movzx ebx,word [word RealModeSSSP+2]
382 mov [word Com32SysSP],di
383 sub edi,ecx ; Allocate space for stack frame
385 sub edi,4*2 ; Return pointer, return value
386 mov [word RealModeSSSP],di
388 add edi,ebx ; Create linear address
390 mov eax,[esp+10*4] ; CS:IP
391 stosd ; Save to stack frame
392 mov eax,com32_cfar_rm.return ; Return seg:off
394 mov esi,[esp+11*4] ; Stack frame
395 mov eax,ecx ; Copy the stack frame
407 mov eax,[word RealModeEAX]
415 RealModeSSSP resd 1 ; Real-mode SS:SP
416 RealModeEAX resd 1 ; Real mode EAX
417 PMESP resd 1 ; Protected-mode ESP
418 Com32SysSP resw 1 ; SP saved during COM32 syscall