📄 memdisk16.asm
字号:
disable_a20: pushad;; Flush the caches;%if DO_WBINVD call try_wbinvd%endif mov bp,[A20Type] add bp,bp ; Convert to word offset.adj5: jmp word [bp+A20DList]a20d_bios: mov ax,2400h pushf ; Some BIOSes muck with IF int 15h popf jmp short a20d_snooze;; Disable the "fast A20 gate";a20d_fast: in al, 092h and al,~03h out 092h, al jmp short a20d_snooze;; Disable the keyboard controller A20 gate;a20d_kbc: call empty_8042_uncond mov al,0D1h out 064h, al ; Command write call empty_8042_uncond mov al,0DDh ; A20 off out 060h, al call empty_8042_uncond ; Wait a bit for it to take effecta20d_snooze: push cx mov cx, disable_wait.delayloop: call a20_test jz .disabled loop .delayloop.disabled: pop cxa20d_dunno:a20d_none: popad ret;; Routine to empty the 8042 KBC controller. If dl != 0; then we will test A20 in the loop and exit if A20 is; suddenly enabled.;empty_8042_uncond: xor dl,dlempty_8042: call a20_test jz .a20_on and dl,dl jnz .done.a20_on: io_delay in al, 064h ; Status port test al,1 jz .no_output io_delay in al, 060h ; Read input jmp short empty_8042.no_output: test al,2 jnz empty_8042 io_delay.done: ret ;; Execute a WBINVD instruction if possible on this CPU;%if DO_WBINVDtry_wbinvd: wbinvd ret%endif section .bss alignb 4PMESP resd 1 ; Protected mode %esp section .idt nobits align=4096 alignb 4096pm_idt resb 4096 ; Protected-mode IDT, followed by interrupt stubspm_entry: equ 0x100000 section .rodata align 4, db 0call32_pmidt: dw 8*256 ; Limit dd pm_idt+CS_BASE ; Addresscall32_rmidt: dw 0ffffh ; Limit dd 0 ; Address section .text;; This is the main entrypoint in this function;init32: mov ebx,call32_call_start+CS_BASE ; Where to go in PMcall32_enter_pm: mov ax,cs mov ds,ax cli mov [SavedSSSP],sp mov [SavedSSSP+2],ss cld call a20_test jnz .a20ok call enable_a20.a20ok: lgdt [call32_gdt] ; Set up GDT lidt [call32_pmidt] ; Set up the IDT mov eax,cr0 or al,1 mov cr0,eax ; Enter protected mode jmp 20h:dword .in_pm+CS_BASE bits 32.in_pm: xor eax,eax ; Available for future use... mov fs,eax mov gs,eax mov al,28h ; Set up data segments mov es,eax mov ds,eax mov ss,eax mov esp,[PMESP+CS_BASE] ; Load protmode %esp if available jmp ebx ; Go to where we need to go;; This is invoked before first dispatch of the 32-bit code, in 32-bit mode;call32_call_start: ; ; Point the stack into low memory ; We have: this segment, bounce buffer, then stack+heap ; mov esp, CS_BASE + 0x20000 + STACK_HEAP_SIZE and esp, ~0xf ; ; Set up the protmode IDT and the interrupt jump buffers ; mov edi,pm_idt+CS_BASE ; Form an interrupt gate descriptor ; WARNING: This is broken if pm_idt crosses a 64K boundary; ; however, it can't because of the alignment constraints. mov ebx,pm_idt+CS_BASE+8*256 mov eax,0x0020ee00 xchg ax,bx xor ecx,ecx inc ch ; ecx <- 256 push ecx.make_idt: stosd add eax,8 xchg eax,ebx stosd xchg eax,ebx loop .make_idt pop ecx ; Each entry in the interrupt jump buffer contains ; the following instructions: ; ; 00000000 60 pushad ; 00000001 B0xx mov al,<interrupt#> ; 00000003 E9xxxxxxxx jmp call32_handle_interrupt mov eax,0xe900b060 mov ebx,call32_handle_interrupt+CS_BASE sub ebx,edi.make_ijb: stosd sub [edi-2],cl ; Interrupt # xchg eax,ebx sub eax,8 stosd xchg eax,ebx loop .make_ijb ; Now everything is set up for interrupts... push dword (BOUNCE_SEG << 4) ; Bounce buffer address push dword call32_syscall+CS_BASE ; Syscall entry point sti ; Interrupts OK now call pm_entry-CS_BASE ; Run the program... ; ... on return ... mov [Return+CS_BASE],eax ; ... fall through to call32_exit ...call32_exit: mov bx,call32_done ; Return to command loopcall32_enter_rm: cli cld mov [PMESP+CS_BASE],esp ; Save exit %esp xor esp,esp ; Make sure the high bits are zero jmp 08h:.in_pm16 ; Return to 16-bit mode first bits 16.in_pm16: mov ax,10h ; Real-mode-like segment mov es,ax mov ds,ax mov ss,ax mov fs,ax mov gs,ax lidt [call32_rmidt] ; Real-mode IDT (rm needs no GDT) mov eax,cr0 and al,~1 mov cr0,eax jmp MY_CS:.in_rm.in_rm: ; Back in real mode mov ax,cs ; Set up sane segments mov ds,ax mov es,ax mov fs,ax mov gs,ax lss sp,[SavedSSSP] ; Restore stack jmp bx ; Go to whereever we need to go...call32_done: call disable_a20 sti mov ax,[Return] ret;; 16-bit support code; bits 16;; 16-bit interrupt-handling code;call32_int_rm: pushf ; Flags on stack push cs ; Return segment push word .cont ; Return address push dword edx ; Segment:offset of IVT entry retf ; Invoke IVT routine.cont: ; ... on resume ... mov ebx,call32_int_resume+CS_BASE jmp call32_enter_pm ; Go back to PM;; 16-bit system call handling code;call32_sys_rm: pop gs pop fs pop es pop ds popad popfd retf ; Invoke routine.return: pushfd pushad push ds push es push fs push gs mov ebx,call32_sys_resume+CS_BASE jmp call32_enter_pm;; 32-bit support code; bits 32;; This is invoked on getting an interrupt in protected mode. At; this point, we need to context-switch to real mode and invoke; the interrupt routine.;; When this gets invoked, the registers are saved on the stack and; AL contains the register number.;call32_handle_interrupt: movzx eax,al xor ebx,ebx ; Actually makes the code smaller mov edx,[ebx+eax*4] ; Get the segment:offset of the routine mov bx,call32_int_rm jmp call32_enter_rm ; Go to real modecall32_int_resume: popad iret;; Syscall invocation. We manifest a structure on the real-mode stack,; containing the call32sys_t structure from <call32.h> as well as; the following entries (from low to high address):; - Target offset; - Target segment; - Return offset; - Return segment (== real mode cs); - Return flags;call32_syscall: pushfd ; Save IF among other things... pushad ; We only need to save some, but... cld movzx edi,word [SavedSSSP+CS_BASE] movzx eax,word [SavedSSSP+CS_BASE+2] sub edi,54 ; Allocate 54 bytes mov [SavedSSSP+CS_BASE],di shl eax,4 add edi,eax ; Create linear address mov esi,[esp+11*4] ; Source regs xor ecx,ecx mov cl,11 ; 44 bytes to copy rep movsd movzx eax,byte [esp+10*4] ; Interrupt number ; ecx == 0 here; adding it to the EA makes the ; encoding smaller mov eax,[ecx+eax*4] ; Get IVT entry stosd ; Save in stack frame mov eax,call32_sys_rm.return + (MY_CS << 16) ; Return seg:offs stosd ; Save in stack frame mov eax,[edi-12] ; Return flags and eax,0x200cd7 ; Mask (potentially) unsafe flags mov [edi-12],eax ; Primary flags entry stosw ; Return flags mov bx,call32_sys_rm jmp call32_enter_rm ; Go to real mode ; On return, the 44-byte return structure is on the ; real-mode stack.call32_sys_resume: movzx esi,word [SavedSSSP+CS_BASE] movzx eax,word [SavedSSSP+CS_BASE+2] mov edi,[esp+12*4] ; Dest regs shl eax,4 add esi,eax ; Create linear address and edi,edi ; NULL pointer? jnz .do_copy.no_copy: mov edi,esi ; Do a dummy copy-to-self.do_copy: xor ecx,ecx mov cl,11 ; 44 bytes rep movsd ; Copy register block add dword [SavedSSSP+CS_BASE],44 ; Remove from stack popad popfd ret ; Return to 32-bit program
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -