Assemble via ELF, to enable future linking
[syslinux.git] / com32.inc
blob999542068601fae8e29303e90e9aaf24267d8902
1 ;; -----------------------------------------------------------------------
2 ;;
3 ;;   Copyright 1994-2008 H. Peter Anvin - All Rights Reserved
4 ;;
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 ;; -----------------------------------------------------------------------
14 ;; com32.inc
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.
29 pm_idt:         equ 0x100000
30 pm_entry:       equ 0x101000
32                 bits 16
33                 section .data
34                 align 2, db 0
35 com32_pmidt:
36                 dw 8*256                ; Limit
37                 dd pm_idt               ; Address
39 com32_rmidt:
40                 dw 0ffffh               ; Limit
41                 dd 0                    ; Address
43                 section .text
44 is_com32_image:
45                 push si                 ; Save file handle
46                 push eax                ; Save file length
48                 call make_plain_cmdline
49                 ; Copy the command line into the low cmdline buffer
50                 mov ax,real_mode_seg
51                 mov fs,ax
52                 mov si,cmd_line_here
53                 mov di,command_line
54                 mov cx,[CmdLinePtr]
55                 inc cx                  ; Include final null
56                 sub cx,si
57                 fs rep movsb
59                 call comboot_setup_api  ; Set up the COMBOOT-style API
61                 mov edi,pm_entry        ; Load address
62                 pop eax                 ; File length
63                 pop si                  ; File handle
64                 xor dx,dx               ; No padding
65                 mov bx,abort_check      ; Don't print dots, but allow abort
66                 call load_high
68 com32_start:
69                 mov ebx,com32_call_start        ; Where to go in PM
71 com32_enter_pm:
72                 cli
73                 mov ax,cs
74                 mov ds,ax
75                 mov [RealModeSSSP],sp
76                 mov [RealModeSSSP+2],ss
77                 cld
78                 call a20_test
79                 jnz .a20ok
80                 call enable_a20
82 .a20ok:
83                 mov byte [bcopy_gdt.TSS+5],89h  ; Mark TSS unbusy
85                 lgdt [bcopy_gdt]        ; We can use the same GDT just fine
86                 lidt [com32_pmidt]      ; Set up the IDT
87                 mov eax,cr0
88                 or al,1
89                 mov cr0,eax             ; Enter protected mode
90                 jmp PM_CS32:.in_pm
92                 bits 32
93 .in_pm:
94                 xor eax,eax             ; Available for future use...
95                 mov fs,eax
96                 mov gs,eax
97                 lldt ax
99                 mov al,PM_DS32          ; Set up data segments
100                 mov es,eax
101                 mov ds,eax
102                 mov ss,eax
104                 mov al,PM_TSS           ; Be nice to Intel's VT by
105                 ltr ax                  ; giving it a valid TR
107                 mov esp,[PMESP]         ; Load protmode %esp if available
108                 jmp ebx                 ; Go to where we need to go
111 ; This is invoked right before the actually starting the COM32
112 ; progam, in 32-bit mode...
114 com32_call_start:
115                 ;
116                 ; Point the stack to the end of (permitted) high memory
117                 ;
118                 mov esp,[word HighMemRsvd]
119                 xor sp,sp               ; Align to a 64K boundary
121                 ;
122                 ; Set up the protmode IDT and the interrupt jump buffers
123                 ; We set these up in the system area at 0x100000,
124                 ; but we could also put them beyond the stack.
125                 ;
126                 mov edi,pm_idt
128                 ; Form an interrupt gate descriptor
129                 mov eax,0x00200000+((pm_idt+8*256)&0x0000ffff)
130                 mov ebx,0x0000ee00+((pm_idt+8*256)&0xffff0000)
131                 xor ecx,ecx
132                 inc ch                          ; ecx <- 256
134                 push ecx
135 .make_idt:
136                 stosd
137                 add eax,8
138                 xchg eax,ebx
139                 stosd
140                 xchg eax,ebx
141                 loop .make_idt
143                 pop ecx
145                 ; Each entry in the interrupt jump buffer contains
146                 ; the following instructions:
147                 ;
148                 ; 00000000 60                pushad
149                 ; 00000001 B0xx              mov al,<interrupt#>
150                 ; 00000003 E9xxxxxxxx        jmp com32_handle_interrupt
152                 mov eax,0e900b060h
153                 mov ebx,com32_handle_interrupt-(pm_idt+8*256+8)
155 .make_ijb:
156                 stosd
157                 sub [edi-2],cl                  ; Interrupt #
158                 xchg eax,ebx
159                 stosd
160                 sub eax,8
161                 xchg eax,ebx
162                 loop .make_ijb
164                 ; Now everything is set up for interrupts...
166                 push dword com32_cfarcall       ; Cfarcall entry point
167                 push dword com32_farcall        ; Farcall entry point
168                 push dword (1 << 16)            ; 64K bounce buffer
169                 push dword (comboot_seg << 4)   ; Bounce buffer address
170                 push dword com32_intcall        ; Intcall entry point
171                 push dword command_line         ; Command line pointer
172                 push dword 6                    ; Argument count
173                 sti                             ; Interrupts OK now
174                 call pm_entry                   ; Run the program...
175                 ; ... on return, fall through to com32_exit ...
177 com32_exit:
178                 mov bx,com32_done       ; Return to command loop
180 com32_enter_rm:
181                 cli
182                 cld
183                 mov [PMESP],esp         ; Save exit %esp
184                 xor esp,esp             ; Make sure the high bits are zero
185                 jmp PM_CS16:.in_pm16    ; Return to 16-bit mode first
187                 bits 16
188 .in_pm16:
189                 mov ax,PM_DS16_RM       ; Real-mode-like segment
190                 mov es,ax
191                 mov ds,ax
192                 mov ss,ax
193                 mov fs,ax
194                 mov gs,ax
196                 lidt [com32_rmidt]      ; Real-mode IDT (rm needs no GDT)
197                 mov eax,cr0
198                 and al,~1
199                 mov cr0,eax
200                 jmp 0:.in_rm
202 .in_rm:                                 ; Back in real mode
203                 mov ax,cs               ; Set up sane segments
204                 mov ds,ax
205                 mov es,ax
206                 mov fs,ax
207                 mov gs,ax
208                 lss sp,[RealModeSSSP]   ; Restore stack
209                 jmp bx                  ; Go to whereever we need to go...
211 com32_done:
212 %if DISABLE_A20
213                 call disable_a20
214 %endif
215                 sti
216                 jmp enter_command
219 ; 16-bit support code
221                 bits 16
224 ; 16-bit interrupt-handling code
226 com32_int_rm:
227                 pushf                           ; Flags on stack
228                 push cs                         ; Return segment
229                 push word .cont                 ; Return address
230                 push dword edx                  ; Segment:offset of IVT entry
231                 retf                            ; Invoke IVT routine
232 .cont:          ; ... on resume ...
233                 mov ebx,com32_int_resume
234                 jmp com32_enter_pm              ; Go back to PM
237 ; 16-bit intcall/farcall handling code
239 com32_sys_rm:
240                 pop gs
241                 pop fs
242                 pop es
243                 pop ds
244                 popad
245                 popfd
246                 mov [cs:Com32SysSP],sp
247                 retf                            ; Invoke routine
248 .return:
249                 ; We clean up SP here because we don't know if the
250                 ; routine returned with RET, RETF or IRET
251                 mov sp,[cs:Com32SysSP]
252                 pushfd
253                 pushad
254                 push ds
255                 push es
256                 push fs
257                 push gs
258                 mov ebx,com32_syscall.resume
259                 jmp com32_enter_pm
262 ; 16-bit cfarcall handing code
264 com32_cfar_rm:
265                 retf
266 .return:
267                 mov sp,[cs:Com32SysSP]
268                 mov [cs:RealModeEAX],eax
269                 mov ebx,com32_cfarcall.resume
270                 jmp com32_enter_pm
273 ; 32-bit support code
275                 bits 32
278 ; This is invoked on getting an interrupt in protected mode.  At
279 ; this point, we need to context-switch to real mode and invoke
280 ; the interrupt routine.
282 ; When this gets invoked, the registers are saved on the stack and
283 ; AL contains the register number.
285 com32_handle_interrupt:
286                 movzx eax,al
287                 xor ebx,ebx             ; Actually makes the code smaller
288                 mov edx,[ebx+eax*4]     ; Get the segment:offset of the routine
289                 mov bx,com32_int_rm
290                 jmp com32_enter_rm      ; Go to real mode
292 com32_int_resume:
293                 popad
294                 iret
297 ; Intcall/farcall invocation.  We manifest a structure on the real-mode stack,
298 ; containing the com32sys_t structure from <com32.h> as well as
299 ; the following entries (from low to high address):
300 ; - Target offset
301 ; - Target segment
302 ; - Return offset
303 ; - Return segment (== real mode cs == 0)
304 ; - Return flags
306 com32_farcall:
307                 pushfd                          ; Save IF among other things...
308                 pushad                          ; We only need to save some, but...
310                 mov eax,[esp+10*4]              ; CS:IP
311                 jmp com32_syscall
314 com32_intcall:
315                 pushfd                          ; Save IF among other things...
316                 pushad                          ; We only need to save some, but...
318                 movzx eax,byte [esp+10*4]       ; INT number
319                 mov eax,[eax*4]                 ; Get CS:IP from low memory
321 com32_syscall:
322                 cld
324                 movzx edi,word [word RealModeSSSP]
325                 movzx ebx,word [word RealModeSSSP+2]
326                 sub edi,54              ; Allocate 54 bytes
327                 mov [word RealModeSSSP],di
328                 shl ebx,4
329                 add edi,ebx             ; Create linear address
331                 mov esi,[esp+11*4]      ; Source regs
332                 xor ecx,ecx
333                 mov cl,11               ; 44 bytes to copy
334                 rep movsd
336                 ; EAX is already set up to be CS:IP
337                 stosd                   ; Save in stack frame
338                 mov eax,com32_sys_rm.return     ; Return seg:offs
339                 stosd                   ; Save in stack frame
340                 mov eax,[edi-12]        ; Return flags
341                 and eax,0x200cd7        ; Mask (potentially) unsafe flags
342                 mov [edi-12],eax        ; Primary flags entry
343                 stosw                   ; Return flags
345                 mov bx,com32_sys_rm
346                 jmp com32_enter_rm      ; Go to real mode
348                 ; On return, the 44-byte return structure is on the
349                 ; real-mode stack, plus the 10 additional bytes used
350                 ; by the target address (see above.)
351 .resume:
352                 movzx esi,word [word RealModeSSSP]
353                 movzx eax,word [word RealModeSSSP+2]
354                 mov edi,[esp+12*4]      ; Dest regs
355                 shl eax,4
356                 add esi,eax             ; Create linear address
357                 and edi,edi             ; NULL pointer?
358                 jnz .do_copy
359 .no_copy:       mov edi,esi             ; Do a dummy copy-to-self
360 .do_copy:       xor ecx,ecx
361                 mov cl,11               ; 44 bytes
362                 rep movsd               ; Copy register block
364                 add dword [word RealModeSSSP],54        ; Remove from stack
366                 popad
367                 popfd
368                 ret                     ; Return to 32-bit program
371 ; Cfarcall invocation.  We copy the stack frame to the real-mode stack,
372 ; followed by the return CS:IP and the CS:IP of the target function.
374 com32_cfarcall:
375                 pushfd
376                 pushad
378                 cld
379                 mov ecx,[esp+12*4]              ; Size of stack frame
381                 movzx edi,word [word RealModeSSSP]
382                 movzx ebx,word [word RealModeSSSP+2]
383                 mov [word Com32SysSP],di
384                 sub edi,ecx             ; Allocate space for stack frame
385                 and edi,~3              ; Round
386                 sub edi,4*2             ; Return pointer, return value
387                 mov [word RealModeSSSP],di
388                 shl ebx,4
389                 add edi,ebx             ; Create linear address
391                 mov eax,[esp+10*4]      ; CS:IP
392                 stosd                   ; Save to stack frame
393                 mov eax,com32_cfar_rm.return    ; Return seg:off
394                 stosd
395                 mov esi,[esp+11*4]      ; Stack frame
396                 mov eax,ecx             ; Copy the stack frame
397                 shr ecx,2
398                 rep movsd
399                 mov ecx,eax
400                 and ecx,3
401                 rep movsb
403                 mov bx,com32_cfar_rm
404                 jmp com32_enter_rm
406 .resume:
407                 popad
408                 mov eax,[word RealModeEAX]
409                 popfd
410                 ret
412                 bits 16
414                 section .bss1
415                 alignb 4
416 RealModeSSSP    resd 1                  ; Real-mode SS:SP
417 RealModeEAX     resd 1                  ; Real mode EAX
418 PMESP           resd 1                  ; Protected-mode ESP
419 Com32SysSP      resw 1                  ; SP saved during COM32 syscall
421                 section .text