2 ;; -----------------------------------------------------------------------
4 ;; Copyright 1994-2008 H. Peter Anvin - All Rights Reserved
6 ;; This program is free software; you can redistribute it and/or modify
7 ;; it under the terms of the GNU General Public License as published by
8 ;; the Free Software Foundation, Inc., 53 Temple Place Ste 330,
9 ;; Boston MA 02111-1307, USA; either version 2 of the License, or
10 ;; (at your option) any later version; incorporated herein by reference.
12 ;; -----------------------------------------------------------------------
17 ;; Routine to initialize and to trampoline into 32-bit
18 ;; protected memory. This code is derived from bcopy32.inc and
19 ;; com32.inc in the main SYSLINUX distribution.
22 %include '../version.gen'
24 MY_CS equ 0x0800 ; Segment address to use
25 CS_BASE equ (MY_CS << 4) ; Corresponding address
27 ; Low memory bounce buffer
28 BOUNCE_SEG equ (MY_CS+0x1000)
32 %define STACK_HEAP_SIZE (128*1024)
34 section .rodata align=16
35 section .data align=16
38 ;; -----------------------------------------------------------------------
39 ;; Kernel image header
40 ;; -----------------------------------------------------------------------
42 section .text ; Must be first in image
45 cmdline times 497 db 0 ; We put the command line here
55 _start: jmp short start
57 db "HdrS" ; Header signature
58 dw 0x0203 ; Header version number
60 realmode_swtch dw 0, 0 ; default_switch, SETUPSEG
61 start_sys_seg dw 0x1000 ; obsolete
62 version_ptr dw memdisk_version-0x200 ; version string ptr
63 type_of_loader db 0 ; Filled in by boot loader
64 loadflags db 1 ; Please load high
65 setup_move_size dw 0 ; Unused
66 code32_start dd 0x100000 ; 32-bit start address
67 ramdisk_image dd 0 ; Loaded ramdisk image address
68 ramdisk_size dd 0 ; Size of loaded ramdisk
69 bootsect_kludge dw 0, 0
72 cmd_line_ptr dd 0 ; Command line
73 ramdisk_max dd 0xffffffff ; Highest allowed ramdisk address
76 ; These fields aren't real setup fields, they're poked in by the
79 b_esdi dd 0 ; ES:DI for boot sector invocation
80 b_edx dd 0 ; EDX for boot sector invocation
84 db "MEMDISK ", VERSION_STR, " ", DATE, 0
86 ;; -----------------------------------------------------------------------
87 ;; End kernel image header
88 ;; -----------------------------------------------------------------------
91 ; Move ourselves down into memory to reduce the risk of conflicts;
92 ; then canonicalize CS to match the other segments.
99 movzx cx,byte [setup_sects]
100 inc cx ; Add one for the boot sector
101 shl cx,7 ; Convert to dwords
109 xor esp,esp ; Stack at top of 64K segment
114 ; Copy the command line, if there is one
117 xor di,di ; Bottom of our own segment (= "boot sector")
118 mov eax,[cmd_line_ptr]
120 jz .endcmd ; No command line
122 shr eax,4 ; Convert to segment
123 and si,0x000F ; Starting offset only
125 mov cx,496 ; Max number of bytes
137 ; Now jump to 32-bit code
142 ; When init32 returns, we have been set up, the new boot sector loaded,
143 ; and we should go and and run the newly loaded boot sector.
145 ; The setup function will have poked values into the setup area.
147 movzx edi,word [cs:b_esdi]
148 mov es,word [cs:b_esdi+2]
152 xor esi,esi ; No partition table involved
153 mov ds,si ; Make all the segments consistent
157 mov esp,0x7C00 ; Good place for SP to start out
159 int 18h ; A far return -> INT 18h
162 ; We enter protected mode, set up a flat 32-bit environment, run rep movsd
163 ; and then exit. IMPORTANT: This code assumes cs == MY_CS.
165 ; This code is probably excessively anal-retentive in its handling of
166 ; segments, but this stuff is painful enough as it is without having to rely
167 ; on everything happening "as it ought to."
171 ; desc base, limit, flags
173 dd (%2 & 0xffff) | ((%1 & 0xffff) << 16)
174 dd (%1 & 0xff000000) | (%2 & 0xf0000) | ((%3 & 0xf0ff) << 8) | ((%1 & 0x00ff0000) >> 16)
178 call32_gdt: dw call32_gdt_size-1 ; Null descriptor - contains GDT
179 .adj1: dd call32_gdt+CS_BASE ; pointer for LGDT instruction
182 ; 0008: Code segment, use16, readable, dpl 0, base CS_BASE, 64K
183 desc CS_BASE, 0xffff, 0x009b
185 ; 0010: Data segment, use16, read/write, dpl 0, base CS_BASE, 64K
186 desc CS_BASE, 0xffff, 0x0093
188 ; 0018: Data segment, use16, read/write, dpl 0, base 0, 4G
189 desc 0, 0xfffff, 0x809b
191 ; 0020: Code segment, use32, read/write, dpl 0, base 0, 4G
192 desc 0, 0xfffff, 0xc09b
194 ; 0028: Data segment, use32, read/write, dpl 0, base 0, 4G
195 desc 0, 0xfffff, 0xc093
197 call32_gdt_size: equ $-call32_gdt
199 err_a20: db 'ERROR: A20 gate not responding!',13,10,0
203 SavedSSSP resd 1 ; Place to save SS:SP
204 Return resd 1 ; Return value
205 A20Test resw 1 ; Space to test A20
210 Target dd 0 ; Target address
211 Target_Seg dw 20h ; Target CS
213 A20Type dw 0 ; Default = unknown
218 ; Routines to enable and disable (yuck) A20. These routines are gathered
219 ; from tips from a couple of sources, including the Linux kernel and
220 ; http://www.x86.org/. The need for the delay to be as large as given here
221 ; is indicated by Donnie Barnes of RedHat, the problematic system being an
222 ; IBM ThinkPad 760EL.
224 ; We typically toggle A20 twice for every 64K transferred.
226 %define io_delay call _io_delay
227 %define IO_DELAY_PORT 80h ; Invalid port (we hope!)
228 %define disable_wait 32 ; How long to wait for a disable
230 %define A20_DUNNO 0 ; A20 type unknown
231 %define A20_NONE 1 ; A20 always on?
232 %define A20_BIOS 2 ; A20 BIOS enable
233 %define A20_KBC 3 ; A20 through KBC
234 %define A20_FAST 4 ; A20 through port 92h
237 A20List dw a20_dunno, a20_none, a20_bios, a20_kbc, a20_fast
238 A20DList dw a20d_dunno, a20d_none, a20d_bios, a20d_kbc, a20d_fast
239 a20_adjust_cnt equ ($-A20List)/2
241 slow_out: out dx, al ; Fall through
243 _io_delay: out IO_DELAY_PORT,al
249 mov byte [A20Tries],255 ; Times to try to make this work
261 ; If the A20 type is known, jump straight to type
264 add bp,bp ; Convert to word offset
265 .adj4: jmp word [bp+A20List]
268 ; First, see if we are on a system with no A20 gate
272 mov byte [A20Type], A20_NONE
277 ; Next, try the BIOS (INT 15h AX=2401h)
280 mov byte [A20Type], A20_BIOS
282 pushf ; Some BIOSes muck with IF
290 ; Enable the keyboard controller A20 gate
293 mov dl, 1 ; Allow early exit
295 jnz a20_done ; A20 live, no need to use KBC
297 mov byte [A20Type], A20_KBC ; Starting KBC command sequence
299 mov al,0D1h ; Write output port
301 call empty_8042_uncond
305 call empty_8042_uncond
307 ; Apparently the UHCI spec assumes that A20 toggle
308 ; ends with a null command (assumed to be for sychronization?)
309 ; Put it here to see if it helps anything...
310 mov al,0FFh ; Null command
312 call empty_8042_uncond
314 ; Verify that A20 actually is enabled. Do that by
315 ; observing a word in low memory and the same word in
316 ; the HMA until they are no longer coherent. Note that
317 ; we don't do the same check in the disable case, because
318 ; we don't want to *require* A20 masking (SYSLINUX should
319 ; work fine without it, if the BIOS does.)
329 ; Running out of options here. Final attempt: enable the "fast A20 gate"
332 mov byte [A20Type], A20_FAST ; Haven't used the KBC yet
335 and al,~01h ; Don't accidentally reset the machine!
348 ; Oh bugger. A20 is not responding. Try frobbing it again; eventually give up
349 ; and report failure to the user.
374 ; A20 unmasked, proceed...
381 ; This routine tests if A20 is enabled (ZF = 0). This routine
382 ; must not destroy any register contents.
388 mov cx,0FFFFh ; HMA = segment 0FFFFh
390 mov cx,32 ; Loop count
394 io_delay ; Serialize, and fix delay
395 cmp ax,[es:A20Test+CS_BASE+10h]
412 add bp,bp ; Convert to word offset
413 .adj5: jmp word [bp+A20DList]
417 pushf ; Some BIOSes muck with IF
420 jmp short a20d_snooze
423 ; Disable the "fast A20 gate"
429 jmp short a20d_snooze
432 ; Disable the keyboard controller A20 gate
435 call empty_8042_uncond
438 out 064h, al ; Write output port
439 call empty_8042_uncond
441 mov al,0DDh ; A20 off
443 call empty_8042_uncond
445 mov al,0FFh ; Null command/synchronization
447 call empty_8042_uncond
449 ; Wait a bit for it to take effect
453 .delayloop: call a20_test
463 ; Routine to empty the 8042 KBC controller. If dl != 0
464 ; then we will test A20 in the loop and exit if A20 is
475 in al, 064h ; Status port
479 in al, 060h ; Read input
488 ; Execute a WBINVD instruction if possible on this CPU
498 PMESP resd 1 ; Protected mode %esp
500 section .idt nobits align=4096
502 pm_idt resb 4096 ; Protected-mode IDT, followed by interrupt stubs
507 pm_entry: equ 0x100000
513 dd pm_idt+CS_BASE ; Address
521 ; This is the main entrypoint in this function
524 mov ebx,call32_call_start+CS_BASE ; Where to go in PM
538 lgdt [call32_gdt] ; Set up GDT
539 lidt [call32_pmidt] ; Set up the IDT
542 mov cr0,eax ; Enter protected mode
543 jmp 20h:dword .in_pm+CS_BASE
547 xor eax,eax ; Available for future use...
551 mov al,28h ; Set up data segments
556 mov esp,[PMESP+CS_BASE] ; Load protmode %esp if available
557 jmp ebx ; Go to where we need to go
560 ; This is invoked before first dispatch of the 32-bit code, in 32-bit mode
564 ; Point the stack into low memory
565 ; We have: this segment, bounce buffer, then stack+heap
567 mov esp, CS_BASE + 0x20000 + STACK_HEAP_SIZE
571 ; Set up the protmode IDT and the interrupt jump buffers
573 mov edi,pm_idt+CS_BASE
575 ; Form an interrupt gate descriptor
576 ; WARNING: This is broken if pm_idt crosses a 64K boundary;
577 ; however, it can't because of the alignment constraints.
578 mov ebx,pm_idt+CS_BASE+8*256
595 ; Each entry in the interrupt jump buffer contains
596 ; the following instructions:
599 ; 00000001 B0xx mov al,<interrupt#>
600 ; 00000003 E9xxxxxxxx jmp call32_handle_interrupt
603 mov ebx,call32_handle_interrupt+CS_BASE
608 sub [edi-2],cl ; Interrupt #
615 ; Now everything is set up for interrupts...
617 push dword (BOUNCE_SEG << 4) ; Bounce buffer address
618 push dword call32_syscall+CS_BASE ; Syscall entry point
619 sti ; Interrupts OK now
620 call pm_entry-CS_BASE ; Run the program...
623 mov [Return+CS_BASE],eax
625 ; ... fall through to call32_exit ...
628 mov bx,call32_done ; Return to command loop
633 mov [PMESP+CS_BASE],esp ; Save exit %esp
634 xor esp,esp ; Make sure the high bits are zero
635 jmp 08h:.in_pm16 ; Return to 16-bit mode first
639 mov ax,10h ; Real-mode-like segment
646 lidt [call32_rmidt] ; Real-mode IDT (rm needs no GDT)
652 .in_rm: ; Back in real mode
653 mov ax,cs ; Set up sane segments
658 lss sp,[SavedSSSP] ; Restore stack
659 jmp bx ; Go to whereever we need to go...
668 ; 16-bit support code
673 ; 16-bit interrupt-handling code
676 pushf ; Flags on stack
677 push cs ; Return segment
678 push word .cont ; Return address
679 push dword edx ; Segment:offset of IVT entry
680 retf ; Invoke IVT routine
681 .cont: ; ... on resume ...
682 mov ebx,call32_int_resume+CS_BASE
683 jmp call32_enter_pm ; Go back to PM
686 ; 16-bit system call handling code
695 retf ; Invoke routine
703 mov ebx,call32_sys_resume+CS_BASE
707 ; 32-bit support code
712 ; This is invoked on getting an interrupt in protected mode. At
713 ; this point, we need to context-switch to real mode and invoke
714 ; the interrupt routine.
716 ; When this gets invoked, the registers are saved on the stack and
717 ; AL contains the register number.
719 call32_handle_interrupt:
721 xor ebx,ebx ; Actually makes the code smaller
722 mov edx,[ebx+eax*4] ; Get the segment:offset of the routine
724 jmp call32_enter_rm ; Go to real mode
731 ; Syscall invocation. We manifest a structure on the real-mode stack,
732 ; containing the call32sys_t structure from <call32.h> as well as
733 ; the following entries (from low to high address):
737 ; - Return segment (== real mode cs)
741 pushfd ; Save IF among other things...
742 pushad ; We only need to save some, but...
745 movzx edi,word [SavedSSSP+CS_BASE]
746 movzx eax,word [SavedSSSP+CS_BASE+2]
747 sub edi,54 ; Allocate 54 bytes
748 mov [SavedSSSP+CS_BASE],di
750 add edi,eax ; Create linear address
752 mov esi,[esp+11*4] ; Source regs
754 mov cl,11 ; 44 bytes to copy
757 movzx eax,byte [esp+10*4] ; Interrupt number
758 ; ecx == 0 here; adding it to the EA makes the
760 mov eax,[ecx+eax*4] ; Get IVT entry
761 stosd ; Save in stack frame
762 mov eax,call32_sys_rm.return + (MY_CS << 16) ; Return seg:offs
763 stosd ; Save in stack frame
764 mov eax,[edi-12] ; Return flags
765 and eax,0x200cd7 ; Mask (potentially) unsafe flags
766 mov [edi-12],eax ; Primary flags entry
770 jmp call32_enter_rm ; Go to real mode
772 ; On return, the 44-byte return structure is on the
775 movzx esi,word [SavedSSSP+CS_BASE]
776 movzx eax,word [SavedSSSP+CS_BASE+2]
777 mov edi,[esp+12*4] ; Dest regs
779 add esi,eax ; Create linear address
780 and edi,edi ; NULL pointer?
782 .no_copy: mov edi,esi ; Do a dummy copy-to-self
783 .do_copy: xor ecx,ecx
785 rep movsd ; Copy register block
787 add dword [SavedSSSP+CS_BASE],44 ; Remove from stack
791 ret ; Return to 32-bit program