wok view linld/stuff/src/VCPI.ASM @ rev 20510

Up acl-dev (2.2.53)
author Pascal Bellard <pascal.bellard@slitaz.org>
date Wed Oct 31 15:13:42 2018 +0100 (2018-10-31)
parents 2e46d946d7a8
children af959ebf1305
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 tss dd ?,? ;enough, we'll never use it anyway
126 ends _BSS
128 segment _TEXT byte public use16 'CODE'
130 ;***************************************************************
131 ;char* prepare_vcpi(void *pagebuf);
132 ;****** Return: AX=>first unused byte in pagebuf
133 ;****** Uses: Flags
134 ;***************************************************************
135 global prepare_vcpi:near
136 proc prepare_vcpi near
138 ;pop ax
139 ;pop bx ;pgbuf
140 ;push bx
141 ;push ax
143 ;Calculate pagedir/page0 addrs, initialize cr3 and pagedir[0]
144 xor eax,eax
145 ; heap_top = prepare_vcpi(malloc_or_die(8*1024+4));
146 mov cx,8*1024+4
147 extrn malloc_or_die:near
148 call malloc_or_die ;keep cx
150 mov edx,cs
151 shl edx,cl ;edx = linear addr of CS
152 mov si,offset gdt_vcpi
153 ; Fix up base of some gdt descriptors
154 ; Note: 'add [dword xx.base0],edx' actually updates 24 bit quantity!
155 ; Do NOT replace with mov!
156 mov ebx,10000h-28h
157 add [dword bx+si+sw2pm_idtr_ptr-gdt_vcpi+28h],edx
158 @@fixup:
159 add [dword bx+si+(gdt_code.base0)-gdt_vcpi+28h],edx
160 add bx,8
161 js @@fixup
162 mov bh,10h
163 add eax,edx
164 and ax,0f000h ;eax = 4k aligned linear addr of pagebuf
165 add eax,ebx ;eax=page0 linear addr
166 add [si+sw2pm_cr3-gdt_vcpi],eax
167 mov edi,eax
168 sub edi,edx
169 mov al,3 ;add present+writable bits
170 mov [bx+di],eax ;stuff it into pagedir[0]
171 push ds
172 pop es ;es:di->page0,es:di+1000h->pagedir
173 ;page directory will use only one entry (4 bytes):
174 ;cr3 => pagedir => page0 => ########
175 ; (1 entry) (1024 => # 4M #
176 ; entries)=> # page #
177 ; => ########
178 ; Return
179 lea ax,[bx+di+4]
180 extrn _heap_top:word
181 mov [_heap_top],ax
182 ;ret
184 endp prepare_vcpi
187 ;***************************************************************
188 ;int get_vcpi_interface();
189 ;****** Return: Z - page mapping for low 640k is 1:1
190 ;****** NZ - otherwise (it's bad)
191 ;****** Uses: Flags
192 ;***************************************************************
193 ;global _get_vcpi_interface:near
194 proc _get_vcpi_interface near
196 ;push si di
198 ; Get and save VCPI pm interface
199 ;mov si,offset gdt_vcpi ;DS:DI => 3 GDT entries for VCPI
200 ;mov di,[si+page0_ofs-gdt_vcpi] ;ES:DI => page0
201 ;push ds
202 ;pop es
203 push di
204 mov ax,0DE01h ;get vcpi pm interface
205 int 67h
206 xchg [vcpi_pm_entry],ebx ;clear ebx
207 ;xor bx,bx
209 ; Check that mapping for low 640k is 1:1
210 pop si ; [page0_ofs]
211 ;cld
212 @@map_chk:
213 lodsd
214 shr eax,12
215 cmp ax,bx
216 jne @@bad
217 inc bx
218 cmp al,((640*1024) shr 12)-1
219 jne @@map_chk
220 ; Return
221 @@bad:
222 ret
223 endp _get_vcpi_interface
226 ;***************************************************************
227 ;void memcpy_vcpi(u32 dstofs,u16 srcseg,u32 srcofs);
228 ;***************************************************************
229 ;****** Copies PAGE_SIZE bytes
230 ;****** Uses: Flags
231 ;***************************************************************
232 global call_pm_routine:near
233 proc call_pm_routine near
235 arg dstofs :dword, \
236 srcseg :word, \
237 srcofs :dword = PARAM_SIZE
239 struc pm_regs
240 $$retaddr dw ?
241 $$f dw ?
242 $$edi dd ?
243 $$esi dd ?
244 $$ebp dd ?
245 $$esp dd ?
246 $$ebx dd ?
247 $$edx dd ?
248 $$ecx dd ?
249 $$eax dd ?
250 ends
252 ;***************************************************************
253 ;****** Helper: goes into 16bit pm and calls routine (addr on stk)
254 ;***************************************************************
255 mov bp,sp ; ss:bp => struct pm_regs
257 xor ax,ax ; IRET stack for return to vm
258 push ax gs ; (9 dwords)
259 push ax fs ;
260 push ax ds ;
261 push ax es ;
262 push ax ss ;
263 push ebp ; esp
264 pushfd ; eflags: IF saved here
265 push ax cs ;
266 push ax ;\eip
267 push offset @@vm_ret ;/
269 ;;mov [saved_xx],xx ;use if your vcpi trashes bp/etc
271 call switch_to_pm
273 ; Now we are in 16-bit protected mode
274 mov ax,SEL_DATA
275 mov ss,ax
276 ;;mov ds,ax
277 ;;mov es,ax
278 ;;mov fs,ax
279 ;;mov gs,ax
280 assume nothing
281 assume cs:DGROUP
283 ;;mov xx,[saved_xx] ;use if your vcpi trashes bp/etc
284 lea sp,[bp-9*4] ;else we can do this trick with bp
286 ; Call the routine (bp points to params on stack if any)
287 mov ax,[(pm_regs bp).$$f] ; ss:bp => struct pm_regs
288 mov bp,[word (pm_regs bp).$$ebp] ;ss:bp => params
289 call ax
291 ; Ok, let's return to vm
292 mov ax,0DE0Ch ; maybe we need whole eax?
293 cli ; to be safe
294 clts ;
295 push SEL_ABS ; vcpi wants ds=all_addrspace
296 pop ds ;
297 call [pword cs:vcpi_pm_entry]
298 @@vm_ret:
299 ; Now we are in vm86 mode. Sregs, esp, eflags (IF) restored from IRET stack
300 ret
302 ;***************************************************************
303 switch_to_pm:
304 assume cs:DGROUP,ds:DGROUP
305 pop [word sw2pm_jumpaddr]
306 ;mov [word sw2pm_jumpaddr+2],0
307 ;mov [word sw2pm_jumpaddr+4],SEL_CODE
308 mov esi,cs
309 shl esi,4
310 addzx_e si,<offset sw2pm_params>
311 mov ax,0DE0Ch ; vcpi: switch to pm
312 cli
313 int 67h
315 ;***************************************************************
316 ;****** Helper: This is where real copy is done
317 ;***************************************************************
318 global do_memcpy_vcpi:near
319 label do_memcpy_vcpi near
321 ; Note: ss:bp => params
322 ; Move data
323 mov ax,SEL_ABS
324 mov ds,ax
325 mov es,ax
326 assume nothing
327 assume cs:DGROUP
329 ; Set up target addr:
330 ; replace page mapping for page at 0 so
331 ; that it points to dstofs
332 mov eax,[dstofs]
333 mov al,03h ; writeable, present
334 call @@set_mapping
335 ;push eax ;X we'll need to restore mapping...
336 xor edi,edi ;es:edi => remapped page
338 ; Set up source addr
339 mov esi,[srcofs]
341 ; Do copying
342 xor ecx,ecx
343 mov ch,4096/256
344 ;cld
345 ;; cmp esi,edi
346 ;; jae @@do_copy
347 ;; add esi,ecx ;src<dst: we must do
348 ;; dec esi ; copy backwards to avoid
349 ;; add edi,ecx ; overwrite bug
350 ;; dec edi ;
351 ;; std ;
352 ;;@@do_copy:
353 db 67h ;address width override for esi/edi
354 rep movsb
355 ;; cld
357 ; Restore page mapping for page at 0
358 ;pop eax ;X
359 @@set_mapping:
360 mov esi,[cs:sw2pm_cr3]
361 xchg [esi-1000h],eax ; restore page0[0]
362 mov ecx,cr3 ; reload TLB cache
363 mov cr3,ecx ;
364 ; Return
365 vcpi_ret:
366 ret
368 endp call_pm_routine
369 assume cs:DGROUP,ds:DGROUP
372 ;***************************************************************
373 ;void _vm2rm();
374 ;***************************************************************
375 ;****** Uses: Flags
376 ;***************************************************************
377 global _vm2rm:near
378 proc _vm2rm near
380 ifdef NO386
381 p8086
382 extrn _vcpi:byte
383 test [_vcpi],-1
384 else
385 p386
386 smsw ax ;SMSW cannot be trapped! :-)
387 and al,1 ;MSW_PE
388 endif
389 jz vcpi_ret
390 p386
391 pop ax
392 push cs ;*
393 push ax
394 push si
395 push ds
396 extrn sssp:word
397 mov [sssp],sp
398 ;mov [sssp+2],ss
400 call switch_to_pm
402 ; Now we are in 16-bit protected mode
403 mov ax,SEL_DATA ; load data sregs with limit >=64k
404 mov ss,ax ; or you may get limit violations
405 mov ds,ax ; later in rm
406 ;mov es,ax ; (actually I prefer 4gig limits :-)
407 ;mov fs,ax ;
408 ;mov gs,ax ;
410 ; Black magic here
411 mov eax,cr0
412 and eax,7ffffffeh ; clear PG,P bits
413 mov cr0,eax ; look mommy, we're in rm now!
414 mov cr3,eax ; flush TLB cache
416 ; Now we are in rm, but not yet: have to restore sregs:
417 lss sp,[dword sssp]; SS
418 pop ds ; DS
419 pop si
420 retf ; CS
422 endp _vm2rm
424 ends _TEXT
426 end
428 ;###### END OF FILE ############################################