wok-6.x view linld/stuff/src/VCPI.ASM @ rev 19906
Up pgrouting (2.4.1)
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Mon Apr 10 16:50:02 2017 +0200 (2017-04-10) |
parents | 6f494adb2c71 |
children | e7715725d95b |
line source
1 ;***************************************************************
2 ;****** This file is distributed under GPL
3 ;***************************************************************
4 ;VDPI init:
5 ;Call char* prepare_vcpi(void *pagebuf) first to
6 ;initialize paging tables needed for vm86.
7 ;pagebuf needs to be 8k+4 bytes. Returns ptr to 1st unused byte.
8 ;Then call int get_vcpi_interface(). Returns 1 if ok, 0 if bad.
9 ;
10 ;VCPI use:
11 ;u32* malloc_vcpi(u32 size) - source in HIMEM.CPP
12 ; Returns ptr to mallocated zero-terminated list of 4k page addrs
13 ; Addresses are sorted in ascending order
14 ; Never fails (will die if not enough mem)
15 ;void read2vcpi(int fd, u32* vp, u32 size,
16 ;void* xfer_buf, u16 xfer_size) - source in HIMEM.CPP
17 ; Reads opened fd data into malloc_vcpi'ed memory
18 ; Dies if file isn't exactly 'size' bytes long
19 ; Needs intermediate buffer of exactly Nx4k bytes
20 ;void memcpy_vcpi(u32 dstofs,u16 srcseg,u32 srcofs)
21 ; copies 4k from conventional memory to VCPI
22 ;void _vm2rm()
23 ; switches you from vm86 to plain real mode
26 ideal
27 %crefref
28 %noincl
29 %nomacs
30 p386
32 ;****** Stuff for declaring descriptors
33 struc descr
34 limit dw ?
35 base0 dw ?
36 base16 db ?
37 type db ?
38 limit16 db ?
39 base24 db ?
40 ends descr
41 macro descriptor name,typ,plevel,present,limit,gran,base
42 ;;name descr <limit and 0ffffh,base and 0ffffh,low (base shr 16),typ or plevel or present,(limit shr 16) or gran,high (base shr 16)>
43 name descr <limit and 0ffffh,base ,0 ,typ or plevel or present,(limit shr 16) or gran,0 >
44 endm
45 ; decriptor types (bit0..4 of descr.type )
46 tss386_avail = 09h
47 data_seg = 00010000b ; data segment
48 expand_down = 00000100b ; =1 limit counts down from base
49 writable = 00000010b ; =1 if write access allowed to data segment
50 code_seg = 00011000b ; code segment
51 conforming = 00000100b ; =1 code can be accesses and executed at any PL
52 readable = 00000010b ; =1 if code also can be read (cannot be ovwritten)
53 ; privilege levels (bit5..6 of descr.type )
54 priv0 = 00000000b
55 priv1 = 00100000b
56 priv2 = 01000000b
57 priv3 = 01100000b
58 ; segment present bit (bit7 of descr.type )
59 is_present = 10000000b
60 not_present = 00000000b
61 ;definition of granularity ( bits6..7 in descr.limit16 )
62 gran_byte = 00000000b
63 gran_page = 10000000b ; 4k granularity
64 use_16 = 00000000b
65 use_32 = 01000000b
67 ;****** rm32,imm16 helpers
68 macro addzx_e rm,i
69 db 66h
70 add rm,i
71 dw 0
72 endm
75 group DGROUP _TEXT,_DATA,_BSS
76 assume cs:DGROUP,ds:DGROUP
78 segment _DATA byte public use16 'DATA'
80 label gdt byte
81 gdt_null descr <?> ;0000
82 gdt_vcpi descr <?> ;0008
83 gdt_vcpi2 descr <?> ;0010
84 gdt_vcpi3 descr <?> ;0018
85 descriptor gdt_abs ,(data_seg+writable),priv0,is_present,0fffffh,(gran_page+use_32),0
86 ;Note: code/data segs must be flagged use16 (i.e. use ip/sp, not eip/esp)
87 ;Note: base addrs will be fixed up in prepare_vcpi()
88 descriptor gdt_code,(code_seg+readable),priv0,is_present,0fffffh,(gran_page+use_16),0
89 descriptor gdt_data,(data_seg+writable),priv0,is_present,0fffffh,(gran_page+use_16),0
90 descriptor gdt_tss ,tss386_avail ,priv0,is_present,0ffh ,gran_byte ,<offset tss>
91 SEL_VCPI = (gdt_vcpi - gdt_null)
92 SEL_TSS = (gdt_tss - gdt_null)
93 SEL_ABS = (gdt_abs - gdt_null)
94 SEL_CODE = (gdt_code - gdt_null)
95 SEL_DATA = (gdt_data - gdt_null)
97 label gdtr pword
98 gdt_lim dw 0ffffh
99 gdt_base dw offset gdt,0
101 ;Note: layout dictated by vcpi api, don't rearrange!
102 label sw2pm_params byte
103 ;Note: base addrs will be fixed up in prepare_vcpi()
104 label pagedir_laddr dword
105 sw2pm_cr3 dd 1000h
106 sw2pm_gdtr_ptr dw offset gdtr,0
107 sw2pm_idtr_ptr dw offset idtr,0
108 sw2pm_ldtr dw 0 ;we don't need it
109 sw2pm_tr dw SEL_TSS ;vcpi thinks we need it... can't set to 0
110 sw2pm_jumpaddr dd 0
111 dw SEL_CODE
113 vcpi_pm_entry dd 0
114 dw SEL_VCPI
116 label idtr pword
117 idt_lim dw 03ffh ;we won't enable ints,
118 idt_base dd 0 ; so let's leave it the same as for rm
120 ends _DATA
122 segment _BSS byte public use16 'BSS'
124 label saved_ss_sp dword
125 saved_sp dw ?
126 saved_ss dw ?
128 tss dd ?,? ;enough, we'll never use it anyway
130 ends _BSS
132 segment _TEXT byte public use16 'CODE'
134 ;***************************************************************
135 ;char* prepare_vcpi(void *pagebuf);
136 ;****** Return: AX=>first unused byte in pagebuf
137 ;****** Uses: Flags
138 ;***************************************************************
139 global _prepare_vcpi:near
140 global prepare_vcpi:near
141 proc _prepare_vcpi near
143 ;pop ax
144 ;pop bx ;pgbuf
145 ;push bx
146 ;push ax
147 prepare_vcpi:
148 ;push esi
149 ;push es
151 ;Calculate pagedir/page0 addrs, initialize cr3 and pagedir[0]
152 xor eax,eax
153 ; heap_top = prepare_vcpi(malloc_or_die(8*1024+4));
154 mov cx,8*1024+4
155 extrn malloc_or_die:near
156 call malloc_or_die
158 mov cl,4
159 mov edx,cs
160 shl edx,cl ;edx = linear addr of CS
161 mov si,offset gdt_vcpi
162 ; Fix up base of some gdt descriptors
163 ; Note: 'add [dword xx.base0],edx' actually updates 24 bit quantity!
164 ; Do NOT replace with mov!
165 mov bx,-28h
166 add [dword bx+si+sw2pm_idtr_ptr-gdt_vcpi+28h],edx
167 @@fixup:
168 add [dword bx+si+(gdt_code.base0)-gdt_vcpi+28h],edx
169 add bl,8
170 js @@fixup
171 add eax,edx
172 dec eax
173 shr eax,cl
174 inc ah
175 mov al,0 ;eax = 4k aligned linear addr of pagebuf
176 mov es,ax ;es:0->page0,es:1000h->pagedir
177 shl eax,cl ;eax=page0 linear addr
178 add [si+sw2pm_cr3-gdt_vcpi],eax
179 mov di,ax
180 mov al,3 ;add present+writable bits
181 mov [es:1000h],eax ;stuff it into pagedir[0]
182 push ds
183 pop es
184 sub di,dx ;ES:DI => page0
185 ;page directory will use only one entry (4 bytes):
186 ;cr3 => pagedir => page0 => ########
187 ; (1 entry) (1024 => # 4M #
188 ; entries)=> # page #
189 ; => ########
190 ; Return
191 ;pop es
192 ;pop esi
193 lea ax,[di+1004h]
194 extrn _heap_top:word
195 mov [_heap_top],ax
196 ;ret
198 endp _prepare_vcpi
201 ;***************************************************************
202 ;int get_vcpi_interface();
203 ;****** Return: Z - page mapping for low 640k is 1:1
204 ;****** NZ - otherwise (it's bad)
205 ;****** Uses: Flags
206 ;***************************************************************
207 global _get_vcpi_interface:near
208 proc _get_vcpi_interface near
210 ;push si di
212 ; Get and save VCPI pm interface
213 ;mov si,offset gdt_vcpi ;DS:DI => 3 GDT entries for VCPI
214 ;mov di,[si+page0_ofs-gdt_vcpi] ;ES:DI => page0
215 ;push ds
216 ;pop es
217 push di
218 mov ax,0DE01h ;get vcpi pm interface
219 int 67h
220 xchg [vcpi_pm_entry],ebx ;clear ebx
221 ;xor bx,bx
223 ; Check that mapping for low 640k is 1:1
224 pop si ; [page0_ofs]
225 ;cld
226 @@map_chk:
227 lodsd
228 shr eax,12
229 cmp ax,bx
230 jne @@bad
231 inc bx
232 cmp al,((640*1024) shr 12)-1
233 jne @@map_chk
234 ; Return
235 @@bad:
236 ret
237 endp _get_vcpi_interface
240 ;***************************************************************
241 ;void memcpy_vcpi(u32 dstofs,u16 srcseg,u32 srcofs);
242 ;***************************************************************
243 ;****** Copies PAGE_SIZE bytes
244 ;****** Uses: Flags
245 ;***************************************************************
246 global call_pm_routine:near
247 proc call_pm_routine near
249 arg dstofs :dword, \
250 srcseg :word, \
251 srcofs :dword = PARAM_SIZE
253 struc pm_regs
254 $$retaddr dw ?
255 $$f dw ?
256 $$edi dd ?
257 $$esi dd ?
258 $$ebp dd ?
259 $$esp dd ?
260 $$ebx dd ?
261 $$edx dd ?
262 $$ecx dd ?
263 $$eax dd ?
264 ends
266 ;***************************************************************
267 ;****** Helper: goes into 16bit pm and calls routine (addr on stk)
268 ;***************************************************************
269 mov bp,sp ; ss:bp => struct pm_regs
271 xor ax,ax ; IRET stack for return to vm
272 push ax gs ; (9 dwords)
273 push ax fs ;
274 push ax ds ;
275 push ax es ;
276 push ax ss ;
277 push ebp ; esp
278 pushfd ; eflags: IF saved here
279 push ax cs ;
280 push ax ;\eip
281 push offset @@vm_ret ;/
283 ;;mov [saved_xx],xx ;use if your vcpi trashes bp/etc
285 call switch_to_pm
287 ; Now we are in 16-bit protected mode
288 mov ax,SEL_DATA
289 mov ss,ax
290 ;;mov ds,ax
291 ;;mov es,ax
292 ;;mov fs,ax
293 ;;mov gs,ax
294 assume nothing
295 assume cs:DGROUP
297 ;;mov xx,[saved_xx] ;use if your vcpi trashes bp/etc
298 lea sp,[bp-9*4] ;else we can do this trick with bp
300 ; Call the routine (bp points to params on stack if any)
301 mov ax,[(pm_regs bp).$$f] ; ss:bp => struct pm_regs
302 mov bp,[word (pm_regs bp).$$ebp] ;ss:bp => params
303 call ax
305 ; Ok, let's return to vm
306 mov ax,0DE0Ch ; maybe we need whole eax?
307 cli ; to be safe
308 clts ;
309 push SEL_ABS ; vcpi wants ds=all_addrspace
310 pop ds ;
311 call [pword cs:vcpi_pm_entry]
312 @@vm_ret:
313 ; Now we are in vm86 mode. Sregs, esp, eflags (IF) restored from IRET stack
314 ret
316 ;***************************************************************
317 switch_to_pm:
318 assume cs:DGROUP,ds:DGROUP
319 pop [word sw2pm_jumpaddr]
320 ;mov [word sw2pm_jumpaddr+2],0
321 ;mov [word sw2pm_jumpaddr+4],SEL_CODE
322 mov esi,cs
323 shl esi,4
324 addzx_e si,<offset sw2pm_params>
325 mov ax,0DE0Ch ; vcpi: switch to pm
326 cli
327 int 67h
329 ;***************************************************************
330 ;****** Helper: This is where real copy is done
331 ;***************************************************************
332 global do_memcpy_vcpi:near
333 label do_memcpy_vcpi near
335 ; Note: ss:bp => params
336 ; Move data
337 mov ax,SEL_ABS
338 mov ds,ax
339 mov es,ax
340 assume nothing
341 assume cs:DGROUP
343 ; Set up target addr:
344 ; replace page mapping for page at 0 so
345 ; that it points to dstofs
346 mov eax,[dstofs]
347 mov al,03h ; writeable, present
348 call @@set_mapping
349 ;push eax ;X we'll need to restore mapping...
350 xor edi,edi ;es:edi => remapped page
352 ; Set up source addr
353 mov esi,[srcofs]
355 ; Do copying
356 xor ecx,ecx
357 mov ch,4096/256
358 ;cld
359 ;; cmp esi,edi
360 ;; jae @@do_copy
361 ;; add esi,ecx ;src<dst: we must do
362 ;; dec esi ; copy backwards to avoid
363 ;; add edi,ecx ; overwrite bug
364 ;; dec edi ;
365 ;; std ;
366 ;;@@do_copy:
367 db 67h ;address width override for esi/edi
368 rep movsb
369 ;; cld
371 ; Restore page mapping for page at 0
372 ;pop eax ;X
373 @@set_mapping:
374 mov esi,[cs:sw2pm_cr3]
375 xchg [esi-1000h],eax ; restore page0[0]
376 mov ecx,cr3 ; reload TLB cache
377 mov cr3,ecx ;
378 ; Return
379 ret
381 endp call_pm_routine
382 assume cs:DGROUP,ds:DGROUP
385 ;***************************************************************
386 ;void _vm2rm();
387 ;***************************************************************
388 ;****** Uses: Flags
389 ;***************************************************************
390 global _vm2rm:near
391 proc _vm2rm near
393 p8086
394 extrn _vcpi:byte
395 test [_vcpi],-1
396 jz @@ret
397 p386
398 push si
399 ;;pushf
400 ;;pushad
401 push cs ;*
402 push ds
403 ;push es
404 mov [saved_ss],ss
405 mov [saved_sp],sp
407 call switch_to_pm
409 ; Now we are in 16-bit protected mode
410 mov ax,SEL_DATA ; load data sregs with limit >=64k
411 mov ss,ax ; or you may get limit violations
412 mov ds,ax ; later in rm
413 ;mov es,ax ; (actually I prefer 4gig limits :-)
414 ;mov fs,ax ;
415 ;mov gs,ax ;
417 ; Black magic here
418 mov eax,cr0
419 and eax,7ffffffeh ; clear PG,P bits
420 mov cr0,eax ; look mommy, we're in rm now!
421 ;;jmp short $+2
422 ;;xor eax,eax
423 mov cr3,eax ; flush TLB cache
425 ; Now we are in rm, but not yet: have to restore sregs:
426 lss sp,[saved_ss_sp]; SS
427 ;pop es ; ES
428 pop ds ; DS
429 ;push cs ;* done earlier
430 push offset @@next
431 retf ; CS
432 @@next:
433 ;;popad
434 ;;popf
435 pop si
436 @@ret:
437 ret ; We don't care much about rest (FS/GS)
439 endp _vm2rm
441 ends _TEXT
443 end
445 ;###### END OF FILE ############################################