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

linld: add 'linld <kernel> <cmdline>' syntax
author Pascal Bellard <pascal.bellard@slitaz.org>
date Fri Dec 02 12:37:59 2016 +0100 (2016-12-02)
parents
children 31c5cbbd9380
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 movzx_e rm,i
69 db 66h
70 mov rm,i
71 dw 0
72 endm
73 macro addzx_e rm,i
74 db 66h
75 add rm,i
76 dw 0
77 endm
80 group DGROUP _TEXT,_DATA
81 assume cs:DGROUP,ds:DGROUP
83 segment _DATA byte public use16 'DATA'
85 tss dd 0,0 ;enough, we'll never use it anyway
87 label gdt byte
88 gdt_null descr <?> ;0000
89 gdt_vcpi descr <?> ;0008
90 gdt_vcpi2 descr <?> ;0010
91 gdt_vcpi3 descr <?> ;0018
92 descriptor gdt_abs ,(data_seg+writable),priv0,is_present,0fffffh,(gran_page+use_32),0
93 ;Note: code/data segs must be flagged use16 (i.e. use ip/sp, not eip/esp)
94 ;Note: base addrs will be fixed up in prepare_vcpi()
95 descriptor gdt_code,(code_seg+readable),priv0,is_present,0fffffh,(gran_page+use_16),0
96 descriptor gdt_data,(data_seg+writable),priv0,is_present,0fffffh,(gran_page+use_16),0
97 descriptor gdt_tss ,tss386_avail ,priv0,is_present,0ffh ,gran_byte ,<offset tss>
98 SEL_VCPI = (gdt_vcpi - gdt_null)
99 SEL_TSS = (gdt_tss - gdt_null)
100 SEL_ABS = (gdt_abs - gdt_null)
101 SEL_CODE = (gdt_code - gdt_null)
102 SEL_DATA = (gdt_data - gdt_null)
104 label gdtr pword
105 gdt_lim dw 0ffffh
106 gdt_base dw offset gdt,0
107 label idtr pword
108 idt_lim dw 03ffh ;we won't enable ints,
109 idt_base dd 0 ; so let's leave it the same as for rm
111 ;Note: layout dictated by vcpi api, don't rearrange!
112 label sw2pm_params byte
113 ;Note: base addrs will be fixed up in prepare_vcpi()
114 sw2pm_cr3 dd ?
115 sw2pm_gdtr_ptr dw offset gdtr,0
116 sw2pm_idtr_ptr dw offset idtr,0
117 sw2pm_ldtr dw 0 ;we don't need it
118 sw2pm_tr dw SEL_TSS ;vcpi thinks we need it... can't set to 0
119 sw2pm_jumpaddr dd ?
120 dw SEL_CODE
122 ;Pagedir/page0 addrs: linear, seg:0 and cs:ofs
123 pagedir_laddr dd ?
124 page0_laddr dd ?
125 ;;pagedir_seg dw ?
126 ;;page0_seg dw ?
127 pagedir_ofs dw ?
128 page0_ofs dw ?
130 vcpi_pm_entry dd ?
131 dw SEL_VCPI
133 label saved_ss_sp dword
134 saved_sp dw ?
135 saved_ss dw ?
138 ends _DATA
141 segment _TEXT byte public use16 'CODE'
143 ;***************************************************************
144 ;char* prepare_vcpi(void *pagebuf);
145 ;****** Return: AX=>first unused byte in pagebuf
146 ;****** Uses: Flags
147 ;***************************************************************
148 global _prepare_vcpi:near
149 global prepare_vcpi:near
150 proc _prepare_vcpi near
152 ;pop ax
153 ;pop bx ;pgbuf
154 ;push bx
155 ;push ax
156 prepare_vcpi:
157 ;push esi
158 ;push es
160 ;Calculate pagedir/page0 addrs, initialize cr3 and pagedir[0]
161 xor eax,eax
162 ; heap_top = prepare_vcpi(malloc_or_die(8*1024+4));
163 mov cx,8*1024+4
164 extrn malloc_or_die:near
165 call malloc_or_die
167 mov cl,4
168 mov edx,cs
169 shl edx,cl ;ebx = linear addr of CS
170 mov bx, offset page0_laddr
171 ; Fix up base of some gdt descriptors
172 ; Note: 'add [dword xx.base0],edx' actually updates 24 bit quantity!
173 ; Do NOT replace with mov!
174 add [dword bx+gdt_base-page0_laddr],edx
175 add [dword bx+sw2pm_gdtr_ptr-page0_laddr],edx
176 add [dword bx+sw2pm_idtr_ptr-page0_laddr],edx
177 add [dword bx+(gdt_code.base0)-page0_laddr],edx
178 add [dword bx+(gdt_data.base0)-page0_laddr],edx
179 add [dword bx+(gdt_tss.base0)-page0_laddr],edx
180 add eax,edx
181 mov esi,0FFFh
182 add eax,esi
183 inc si ;esi=00001000h
185 shr eax,cl
186 mov al,0 ;eax = 4k aligned linear addr of pagebuf
187 mov es,ax ;es:0->page0,es:1000h->pagedir
188 shl eax,cl ;eax=page0 linear addr
189 mov [bx+page0_laddr-page0_laddr],eax
190 mov al,3 ;add present+writable bits
191 mov [es:si],eax ;stuff it into pagedir[0]
193 add eax,esi
194 mov al,0
195 mov [bx+pagedir_laddr-page0_laddr],eax
196 ;page directory will use only one entry (4 bytes):
197 ;cr3 => pagedir => page0 => ########
198 ; (1 entry) (1024 => # 4M #
199 ; entries)=> # page #
200 ; => ########
201 mov [bx+sw2pm_cr3-page0_laddr],eax
202 sub eax,edx ;ax = offset in CS of pagedir
203 mov [bx+pagedir_ofs-page0_laddr],ax
204 sub ax,si ;ax-=1000
205 mov [bx+page0_ofs-page0_laddr],ax
206 ; Return
207 ;pop es
208 ;pop esi
209 add ax,1004h
210 extrn _heap_top:word
211 mov [_heap_top],ax
212 ;ret
214 endp _prepare_vcpi
217 ;***************************************************************
218 ;int get_vcpi_interface();
219 ;****** Return: AX=1 - page mapping for low 640k is 1:1
220 ;****** AX=0 - otherwise (it's bad)
221 ;****** Uses: Flags
222 ;***************************************************************
223 global _get_vcpi_interface:near
224 proc _get_vcpi_interface near
226 ;push si di
228 ; Get and save VCPI pm interface
229 mov si,offset gdt_vcpi ;DS:DI => 3 GDT entries for VCPI
230 mov di,[si+page0_ofs-gdt_vcpi] ;ES:DI => page0
231 push ds
232 pop es
233 mov ax,0DE01h ;get vcpi pm interface
234 int 67h
235 mov [vcpi_pm_entry],ebx
237 ; Check that mapping for low 640k is 1:1
238 mov si,[page0_ofs]
239 xor bx,bx
240 cld
241 @@map_chk:
242 lodsd
243 shr eax,12
244 cmp ax,bx
245 stc
246 jne @@bad
247 inc bx
248 cmp ax,((640*1024) shr 12)-1
249 jne @@map_chk
250 ; Return
251 @@bad:
252 sbb ax,ax
253 inc ax ; update Z
254 ;pop di si
255 ret
256 endp _get_vcpi_interface
259 ;***************************************************************
260 ;void memcpy_vcpi(u32 dstofs,u16 srcseg,u32 srcofs);
261 ;***************************************************************
262 ;****** Copies PAGE_SIZE bytes
263 ;****** Uses: Flags
264 ;***************************************************************
265 global call_pm_routine:near
266 proc call_pm_routine near
268 arg dstofs :dword, \
269 srcseg :word, \
270 srcofs :dword = PARAM_SIZE
272 struc pm_regs
273 $$retaddr dw ?
274 $$f dw ?
275 $$edi dd ?
276 $$esi dd ?
277 $$ebp dd ?
278 $$esp dd ?
279 $$ebx dd ?
280 $$edx dd ?
281 $$ecx dd ?
282 $$eax dd ?
283 ends
285 ;***************************************************************
286 ;****** Helper: goes into 16bit pm and calls routine (addr on stk)
287 ;***************************************************************
288 mov bp,sp ; ss:bp => struct pm_regs
290 xor ax,ax ; IRET stack for return to vm
291 push ax gs ; (9 dwords)
292 push ax fs ;
293 push ax ds ;
294 push ax es ;
295 push ax ss ;
296 push ebp ; esp
297 pushfd ; eflags: IF saved here
298 push ax cs ;
299 push ax ;\eip
300 push offset @@vm_ret ;/
302 cli
303 ;;mov [saved_xx],xx ;use if your vcpi trashes bp/etc
305 movzx_e <[word sw2pm_jumpaddr]>,<offset @@pm_entry>
306 ;mov [word sw2pm_jumpaddr+4],SEL_CODE
307 mov esi,cs
308 shl esi,4
309 addzx_e si,<offset sw2pm_params>
310 mov ax,0DE0Ch ; vcpi: switch to pm
311 int 67h
312 @@pm_entry:
313 ; Now we are in 16-bit protected mode
314 mov ax,SEL_DATA
315 mov ss,ax
316 ;;mov ds,ax
317 ;;mov es,ax
318 ;;mov fs,ax
319 ;;mov gs,ax
320 assume nothing
321 assume cs:DGROUP
323 ;;mov xx,[saved_xx] ;use if your vcpi trashes bp/etc
324 lea sp,[bp-9*4] ;else we can do this trick with bp
326 ; Call the routine (bp points to params on stack if any)
327 mov ax,[(pm_regs bp).$$f] ; ss:bp => struct pm_regs
328 mov bp,[word (pm_regs bp).$$ebp] ;ss:bp => params
329 call ax
331 ; Ok, let's return to vm
332 cli ; to be safe
333 clts ;
334 push SEL_ABS ; vcpi wants ds=all_addrspace
335 pop ds ;
336 mov ax,0DE0Ch ; maybe we need whole eax?
337 call [pword cs:vcpi_pm_entry]
338 @@vm_ret:
339 ; Now we are in vm86 mode. Sregs, esp, eflags (IF) restored from IRET stack
340 ret
342 ;***************************************************************
343 ;****** Helper: This is where real copy is done
344 ;***************************************************************
345 global do_memcpy_vcpi:near
346 label do_memcpy_vcpi near
348 ; Note: ss:bp => params
349 ; Move data
350 mov cx,SEL_ABS
351 mov ds,cx
352 mov es,cx
353 assume nothing
354 assume cs:DGROUP
356 ; Set up target addr:
357 ; replace page mapping for page at 0 so
358 ; that it points to dstofs
359 mov esi,[cs:page0_laddr]
360 mov eax,[dstofs]
361 mov al,03h ; writeable, present
362 xchg [esi],eax ; replace page0[0]
363 ;push eax ;X we'll need to restore mapping...
364 mov ecx,cr3 ; reload TLB cache
365 mov cr3,ecx ;
366 xor edi,edi ;es:edi => remapped page
368 ; Set up source addr
369 mov esi,[srcofs]
371 ; Do copying
372 mov ecx,4096/2
373 cld
374 ;; cmp esi,edi
375 ;; jae @@do_copy
376 ;; add esi,ecx ;src<dst: we must do
377 ;; dec esi ; copy backwards to avoid
378 ;; add edi,ecx ; overwrite bug
379 ;; dec edi ;
380 ;; std ;
381 ;;@@do_copy:
382 db 67h ;address width override for esi/edi
383 rep movsw
384 ;; cld
386 ; Restore page mapping for page at 0
387 ;pop eax ;X
388 mov esi,[cs:page0_laddr]
389 mov [esi],eax ; restore page0[0]
390 mov ecx,cr3 ; reload TLB cache
391 mov cr3,ecx ;
392 ; Return
393 ret
395 endp call_pm_routine
396 assume cs:DGROUP,ds:DGROUP
399 ;***************************************************************
400 ;void _vm2rm();
401 ;***************************************************************
402 ;****** Uses: Flags
403 ;***************************************************************
404 global _vm2rm:near
405 proc _vm2rm near
407 push esi
408 ;;pushf
409 ;;cli
410 ;;pushad
411 push cs ;*
412 push ds
413 mov [saved_ss],ss
414 mov [saved_sp],sp
416 movzx_e <[word sw2pm_jumpaddr]>,<offset @@pm_entry>
417 ;mov [word sw2pm_jumpaddr+4],SEL_CODE
418 mov esi,cs
419 shl esi,4
420 addzx_e si,<offset sw2pm_params>
421 mov ax,0DE0Ch ; vcpi: switch to pm
422 int 67h
423 ; Now we are in 16-bit protected mode
424 @@pm_entry:
425 mov ax,SEL_DATA ; load data sregs with limit >=64k
426 mov ss,ax ; or you may get limit violations
427 mov ds,ax ; later in rm
428 mov es,ax ; (actually I prefer 4gig limits :-)
429 mov fs,ax ;
430 mov gs,ax ;
432 ; Black magic here
433 mov eax,cr0
434 and eax,7ffffffeh ; clear PG,P bits
435 mov cr0,eax ; look mommy, we're in rm now!
436 ;;jmp short $+2
437 ;;xor eax,eax
438 mov cr3,eax ; flush TLB cache
440 ; Now we are in rm, but not yet: have to restore sregs:
441 lss sp,[saved_ss_sp]; SS
442 pop ds ; DS
443 ;push cs ;* done earlier
444 push offset @@next
445 retf ; CS
446 @@next:
447 ;;popad
448 ;;popf
449 pop esi
450 ret ; We don't care much about rest (ES/FS/GS)
451 endp _vm2rm
453 ends _TEXT
455 end
457 ;###### END OF FILE ############################################