Merge branch 'master' into fsc
[profile/ivi/syslinux.git] / core / callback.inc
1 ;; -----------------------------------------------------------------------
2 ;;
3 ;;   Copyright 1994-2009 H. Peter Anvin - All Rights Reserved
4 ;;   Copyright 2009 Intel Corporation; author: H. Peter Anvin
5 ;;
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.
11 ;;
12 ;; -----------------------------------------------------------------------
13
14 ;;
15 ;; callback.inc
16 ;;
17 ;; Callbacks from 32-bit mode to 16-bit mode
18 ;;
19
20 ;
21 ; 16-bit intcall/farcall handling code
22 ;
23
24 ;
25 ; 32-bit support code
26 ;
27                 bits 32
28                 section .text
29
30 ;
31 ; Intcall/farcall invocation.  We manifest a structure on the real-mode stack,
32 ; containing the com32sys_t structure from <com32.h> as well as
33 ; the following entries (from low to high address):
34 ; - Target offset
35 ; - Target segment
36 ; - Return offset
37 ; - Return segment (== real mode cs == 0)
38 ; - Return flags
39 ;
40                 global core_farcall
41 core_farcall:
42                 mov eax,[esp+1*4]               ; CS:IP
43                 jmp core_syscall
44
45                 global core_intcall
46 core_intcall:
47                 movzx eax,byte [esp+1*4]        ; INT number
48                 mov eax,[eax*4]                 ; Get CS:IP from low memory
49
50 core_syscall:
51                 pushfd                          ; Save IF among other things...
52                 push ebx
53                 push ebp
54                 push esi
55                 push edi
56                 push dword [CallbackSP]
57
58                 cld
59
60                 movzx edi,word [word RealModeSSSP]
61                 movzx ebx,word [word RealModeSSSP+2]
62                 sub edi,54              ; Allocate 54 bytes
63                 mov [word RealModeSSSP],di
64                 shl ebx,4
65                 add edi,ebx             ; Create linear address
66
67                 mov esi,[esp+8*4]       ; Source regs
68                 xor ecx,ecx
69                 mov cl,11               ; 44 bytes to copy
70                 rep movsd
71
72                 ; EAX is already set up to be CS:IP
73                 stosd                   ; Save in stack frame
74                 mov eax,.rm_return      ; Return seg:offs
75                 stosd                   ; Save in stack frame
76                 mov eax,[edi-12]        ; Return flags
77                 and eax,0x200cd7        ; Mask (potentially) unsafe flags
78                 mov [edi-12],eax        ; Primary flags entry
79                 stosw                   ; Return flags
80
81                 mov bx,.rm
82                 jmp enter_rm    ; Go to real mode
83
84                 bits 16
85                 section .text16
86 .rm:
87                 pop gs
88                 pop fs
89                 pop es
90                 pop ds
91                 popad
92                 popfd
93                 mov [cs:CallbackSP],sp
94                 retf                            ; Invoke routine
95
96 .rm_return:
97                 ; We clean up SP here because we don't know if the
98                 ; routine returned with RET, RETF or IRET
99                 mov sp,[cs:CallbackSP]
100                 pushfd
101                 pushad
102                 push ds
103                 push es
104                 push fs
105                 push gs
106                 mov ebx,.pm_return
107                 jmp enter_pm
108
109                 ; On return, the 44-byte return structure is on the
110                 ; real-mode stack, plus the 10 additional bytes used
111                 ; by the target address (see above.)
112                 bits 32
113                 section .text
114 .pm_return:
115                 movzx esi,word [word RealModeSSSP]
116                 movzx eax,word [word RealModeSSSP+2]
117                 mov edi,[esp+9*4]       ; Dest regs
118                 shl eax,4
119                 add esi,eax             ; Create linear address
120                 and edi,edi             ; NULL pointer?
121                 jnz .do_copy
122 .no_copy:       mov edi,esi             ; Do a dummy copy-to-self
123 .do_copy:       xor ecx,ecx
124                 mov cl,11               ; 44 bytes
125                 rep movsd               ; Copy register block
126
127                 add dword [word RealModeSSSP],54
128                                         ; Remove from stack
129
130                 pop dword [CallbackSP]
131                 pop edi
132                 pop esi
133                 pop ebp
134                 pop ebx
135                 popfd
136                 ret                     ; Return to 32-bit program
137
138 ;
139 ; Cfarcall invocation.  We copy the stack frame to the real-mode stack,
140 ; followed by the return CS:IP and the CS:IP of the target function.
141 ;
142                 global core_cfarcall
143 core_cfarcall:
144                 pushfd                          ; Save IF among other things...
145                 push ebx
146                 push ebp
147                 push esi
148                 push edi
149                 push dword [CallbackSP]
150
151                 cld
152                 mov ecx,[esp+9*4]               ; Size of stack frame
153
154                 movzx edi,word [word RealModeSSSP]
155                 movzx ebx,word [word RealModeSSSP+2]
156                 mov [word CallbackSP],di
157                 sub edi,ecx             ; Allocate space for stack frame
158                 and edi,~3              ; Round
159                 sub edi,4*2             ; Return pointer, return value
160                 mov [word RealModeSSSP],di
161                 shl ebx,4
162                 add edi,ebx             ; Create linear address
163
164                 mov eax,[esp+7*4]       ; CS:IP
165                 stosd                   ; Save to stack frame
166                 mov eax,.rm_return      ; Return seg:off
167                 stosd
168                 mov esi,[esp+8*4]       ; Stack frame
169                 mov eax,ecx             ; Copy the stack frame
170                 shr ecx,2
171                 rep movsd
172                 mov ecx,eax
173                 and ecx,3
174                 rep movsb
175
176                 mov bx,.rm
177                 jmp enter_rm
178
179                 bits 16
180                 section .text16
181 .rm:
182                 retf
183 .rm_return:
184                 mov sp,[cs:CallbackSP]
185                 mov esi,eax
186                 mov ebx,.pm_return
187                 jmp enter_pm
188
189                 bits 32
190                 section .text
191 .pm_return:
192                 mov eax,esi
193                 ; EDX already set up to be the RM return value
194                 pop dword [CallbackSP]
195                 pop ebx
196                 pop ebp
197                 pop esi
198                 pop edi
199                 popfd
200                 ret
201
202                 bits 16
203                 section .bss16
204                 alignb 4
205 CallbackSP      resd 1                  ; SP saved during callback
206
207                 bits 16
208                 section .text16