summaryrefslogtreecommitdiff
path: root/i386/dos/i16/i16_vcpi.c
blob: e021d6b21d1d0c200b6d566a59a207cce241c961 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
/* 
 * Copyright (c) 1995-1994 The University of Utah and
 * the Computer Systems Laboratory at the University of Utah (CSL).
 * All rights reserved.
 *
 * Permission to use, copy, modify and distribute this software is hereby
 * granted provided that (1) source code retains these copyright, permission,
 * and disclaimer notices, and (2) redistributions including binaries
 * reproduce the notices in supporting documentation, and (3) all advertising
 * materials mentioning features or use of this software display the following
 * acknowledgement: ``This product includes software developed by the
 * Computer Systems Laboratory at the University of Utah.''
 *
 * THE UNIVERSITY OF UTAH AND CSL ALLOW FREE USE OF THIS SOFTWARE IN ITS "AS
 * IS" CONDITION.  THE UNIVERSITY OF UTAH AND CSL DISCLAIM ANY LIABILITY OF
 * ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
 *
 * CSL requests users of this software to return to csl-dist@cs.utah.edu any
 * improvements that they make and grant CSL redistribution rights.
 *
 *      Author: Bryan Ford, University of Utah CSL
 */

#include <mach/boolean.h>
#include <mach/vm_param.h>
#include <mach/machine/code16.h>
#include <mach/machine/vm_types.h>
#include <mach/machine/paging.h>
#include <mach/machine/eflags.h>
#include <mach/machine/proc_reg.h>
#include <mach/machine/far_ptr.h>
#include <mach/machine/vcpi.h>
#include <mach/machine/asm.h>

#include "config.h"
#include "i16.h"
#include "i16_dos.h"
#include "cpu.h"
#include "real.h"
#include "debug.h"
#include "vm_param.h"

#ifdef ENABLE_VCPI

static boolean_t ems_page_allocated;
static unsigned short ems_handle;

static vm_offset_t vcpi_pdir, vcpi_ptable0;

struct far_pointer_32 vcpi_pmode_entry = {0, VCPI_CS};
struct vcpi_switch_data vcpi_switch_data;

static struct pseudo_descriptor gdt_pdesc, idt_pdesc;

static boolean_t pic_reprogrammed;

/* Save area for the DOS interrupt vectors
   that used to be in the place we relocated the master PIC to.  */
static struct far_pointer_16 master_save_vecs[8];


#ifdef ENABLE_PAGING
#define VCPI_PAGING_INIT(pdir_pa, first_unmapped_pa) vcpi_paging_init(pdir_pa, first_unmapped_pa)
#else
#define VCPI_PAGING_INIT(pdir_pa, first_unmapped_pa) ((void)0)
#endif

#ifdef ENABLE_KERNEL_LDT
#define KERNEL_LDT_INIT() (vcpi_switch_data.ldt_sel = KERNEL_LDT)
#else
#define KERNEL_LDT_INIT() ((void)0)
#endif


CODE16

static void i16_vcpi_switch_to_pmode()
{
	extern vm_offset_t boot_image_pa;

	i16_cli();

	i16_assert(i16_get_ds() == i16_get_cs());
	i16_assert(i16_get_es() == i16_get_cs());
	i16_assert(i16_get_ss() == i16_get_cs());

	/* Make sure the TSS isn't marked busy.  */
	cpu[0].tables.gdt[KERNEL_TSS_IDX].access &= ~ACC_TSS_BUSY;

	/* Ask the VCPI server to switch to protected mode.  */
	asm volatile("
		movl	%%esp,%%edx
		int	$0x67
	"SEXT(pmode_return)":
		movl	%%edx,%%esp
		movw	%2,%%dx
		movw	%%dx,%%ss
		movw	%%dx,%%ds
		movw	%%dx,%%es
		xorw	%%dx,%%dx
		movw	%%dx,%%fs
		movw	%%dx,%%gs
	" :
	  : "a" ((unsigned short)0xde0c),
	    "S" (boot_image_pa + (vm_offset_t)&vcpi_switch_data),
	    "i" (KERNEL_DS)
	  : "eax", "edx", "esi");

	/* Make sure the direction flag is still clear.  */
	i16_cld();
}

static void i16_vcpi_switch_to_real_mode()
{
	i16_cli();

	/* As requested by VCPI spec... */
	i16_clts();

	/* Perform the switch.  */
	asm volatile("
		movl	%%esp,%%edx
		pushl	%1
		pushl	%1
		pushl	%1
		pushl	%1
		pushl	%1
		pushl	%%edx
		pushl	$0
		pushl	%1
		pushl	$1f
		movw	%2,%%ds
		lcall	%%ss:"SEXT(vcpi_pmode_entry)"
	1:
	" :
	  : "a" ((unsigned short)0xde0c),
	    "r" ((unsigned)real_cs),
	    "r" ((unsigned short)LINEAR_DS)
	  : "eax", "edx");

	i16_assert(!(i16_get_eflags() & EFL_IF));
	i16_assert(i16_get_ds() == i16_get_cs());
	i16_assert(i16_get_es() == i16_get_cs());
	i16_assert(i16_get_ss() == i16_get_cs());

	/* Make sure the direction flag is still clear.  */
	i16_cld();
}

CODE32

static void vcpi_real_int(int intnum, struct real_call_data *rcd)
{
	do_16bit(
		unsigned int eflags;

		i16_vcpi_switch_to_real_mode();
		i16_real_int(intnum, rcd);
		i16_vcpi_switch_to_pmode();
	);
}

static void vcpi_exit(int rc)
{
	do_16bit(
		i16_vcpi_switch_to_real_mode();
		i16_exit(rc);
		while (1);
	);
}

CODE16

static inline void
i16_vcpi_set_int_vecs(unsigned short master, unsigned short slave)
{
	unsigned short rc;

	i16_assert(!(get_eflags() & EFL_IF));
	asm volatile("int $0x67"
		: "=a" (rc)
		: "a" ((unsigned short)0xde0b),
		  "b" ((unsigned short)master),
		  "c" ((unsigned short)slave));
	i16_assert((rc & 0xff00) == 0);
	i16_assert(!(get_eflags() & EFL_IF));
}

/* Find a (hopefully) empty set of interrupt vectors
   to use for the master hardware interrupts.
   We assume that eight interrupt vectors in a row
   that all have the same value are unused.
   If VCPI servers weren't so brain-damaged
   and took care of this during interrupt reflection
   (like we do when running in raw mode),
   this kludgery wouldn't be needed...  */
static int i16_find_free_vec_range()
{
	/* i will track the first vector in a range;
	   j will track the last.  */
	int i, j;
	struct far_pointer_16 iv, jv;

	j = 0xff;
	i16_dos_get_int_vec(j, &jv);

	for (i = j-1; ; i--)
	{
		if (i == 0x50)
		{
			/* No completely free sets found.
			   Stop here and just use 0x50-0x57.  */
			break;
		}

		i16_dos_get_int_vec(i, &iv);
		if ((iv.ofs != jv.ofs) || (iv.seg != jv.seg))
		{
			/* Vector contents changed.  */
			j = i;
			jv = iv;
			continue;
		}

		if ((j-i+1 >= 8) && ((i & 7) == 0))
		{
			/* Found a free range.  */
			break;
		}
	}

	return i;
}

void i16_vcpi_check()
{
	extern vm_offset_t dos_mem_phys_free_mem;
	extern vm_offset_t dos_mem_phys_free_size;
	extern void pmode_return();
	extern vm_offset_t boot_image_pa;
	extern void (*i16_switch_to_real_mode)();
	extern void (*i16_switch_to_pmode)();

	unsigned short rc;
	unsigned short first_free_pte;
	unsigned short vcpi_ver;

	i16_assert(boot_image_pa == kvtophys(0));

	/* Check for presence of EMM driver.  */
	{
		int dev_info, out_status;
		int fh;

		fh = i16_dos_open("EMMXXXX0", 0);
		if (fh < 0)
			return;
		dev_info = i16_dos_get_device_info(fh);
		out_status = i16_dos_get_output_status(fh);
		i16_dos_close(fh);
		if ((dev_info < 0) || !(dev_info & 0x80)
		    || (out_status != 0xff))
			return;
	}

	/* Allocate an EMS page to force the EMM to be turned on.
	   If it fails, keep going anyway -
	   it may simply mean all the EMS pages are allocated.  */
	asm volatile("int $0x67"
			: "=a" (rc),
			  "=d" (ems_handle)
			: "a" ((unsigned short)0x4300),
			  "b" ((unsigned short)1));
	if (!(rc & 0xff00))
		ems_page_allocated = TRUE;

	/* Check for VCPI.  */
	asm volatile("int $0x67" : "=a" (rc), "=b" (vcpi_ver) : "a" ((unsigned short)0xde00));
	if (rc & 0xff00)
		return;
	i16_assert(vcpi_ver >= 0x0100);

	/* OK, it's there - we're now committed to using VCPI.  */
	i16_switch_to_real_mode = i16_vcpi_switch_to_real_mode;
	i16_switch_to_pmode = i16_vcpi_switch_to_pmode;
	real_int = vcpi_real_int;
	real_exit = vcpi_exit;

	do_debug(i16_puts("VCPI detected"));

	/* Allocate a page directory and page table from low DOS memory.  */
	{
		vm_offset_t new_dos_mem;

		new_dos_mem = ((dos_mem_phys_free_mem + PAGE_MASK) & ~PAGE_MASK)
				+ PAGE_SIZE*2;
		if ((!dos_mem_phys_free_mem)
		    || (new_dos_mem - dos_mem_phys_free_mem
		        > dos_mem_phys_free_size))
			i16_die("not enough low DOS memory available");
		dos_mem_phys_free_size -= new_dos_mem - dos_mem_phys_free_mem;
		dos_mem_phys_free_mem = new_dos_mem;
		vcpi_pdir = new_dos_mem - PAGE_SIZE*2;
		vcpi_ptable0 = vcpi_pdir + PAGE_SIZE;
	}

	/* Initialize them.  */
	{
		int i;
		pt_entry_t pde0 = vcpi_ptable0
			| INTEL_PTE_VALID | INTEL_PTE_WRITE | INTEL_PTE_USER;

		set_fs(vcpi_pdir >> 4);
		asm volatile("movl %0,%%fs:(0)" : : "r" (pde0));
		for (i = 1; i < NPDES + NPTES; i++)
			asm volatile("movl $0,%%fs:(,%0,4)" : : "r" (i));
	}

	/* Initialize the protected-mode interface.  */
	asm volatile("
		pushw	%%es
		movw	%4,%%es
		int	$0x67
		popw	%%es
	"
		: "=a" (rc),
		  "=b" (vcpi_pmode_entry.ofs),
		  "=D" (first_free_pte)
		: "a" ((unsigned short)0xde01),
		  "r" ((unsigned short)(vcpi_ptable0 >> 4)),
		  "D" (0),
		  "S" (&cpu[0].tables.gdt[VCPI_CS_IDX]));
	i16_assert((rc & 0xff00) == 0);
	i16_assert(get_ds() == get_cs());
	i16_assert(get_es() == get_cs());

#ifdef DEBUG
	/* Sanity check: make sure the server did what it was supposed to do.  */

	i16_assert((cpu[0].tables.gdt[VCPI_CS_IDX].access & ACC_P|ACC_CODE) == ACC_P|ACC_CODE);
	if (cpu[0].tables.gdt[VCPI_CS_IDX].granularity & SZ_G)
		i16_assert(vcpi_pmode_entry.ofs <
			   (((vm_offset_t)cpu[0].tables.gdt[VCPI_CS_IDX].limit_high << 28)
			    | ((vm_offset_t)cpu[0].tables.gdt[VCPI_CS_IDX].limit_low << 12)
			    | (vm_offset_t)0xfff));
	else
		i16_assert(vcpi_pmode_entry.ofs <
			   (((vm_offset_t)cpu[0].tables.gdt[VCPI_CS_IDX].limit_high << 16)
			    | (vm_offset_t)cpu[0].tables.gdt[VCPI_CS_IDX].limit_low));

	i16_assert(first_free_pte/sizeof(pt_entry_t) >= 1*1024*1024/PAGE_SIZE);
	i16_assert(first_free_pte/sizeof(pt_entry_t) <= 4*1024*1024/PAGE_SIZE);

	{
		int i;

		for (i = 0; i < 1*1024*1024/PAGE_SIZE; i++)
		{
			pt_entry_t entry;

			set_ds(vcpi_ptable0 >> 4);
			entry = ((pt_entry_t*)0)[i];
			set_ds(get_cs());
			i16_assert(entry & INTEL_PTE_VALID);
			if (i < 0xf0000/PAGE_SIZE)
				i16_assert(entry & INTEL_PTE_WRITE);
			i16_assert(entry & INTEL_PTE_USER);
			i16_assert(!(entry & INTEL_PTE_AVAIL));
		}
	}
#endif /* DEBUG */

	/* Find the VCPI server's hardware interrupt vector mappings.  */
	asm volatile("int $0x67"
		: "=a" (rc),
		  "=b" (irq_master_base),
		  "=c" (irq_slave_base)
		: "a" ((unsigned short)0xde0a));
	i16_assert((rc & 0xff00) == 0);
	irq_master_base &= 0xffff;
	irq_slave_base &= 0xffff;
	i16_assert((irq_master_base & 7) == 0);
	i16_assert((irq_master_base == 0x08) || (irq_master_base >= 0x20));
	i16_assert((irq_slave_base & 7) == 0);
	i16_assert(irq_slave_base >= 0x20);

	/* If they're the usual DOS values, change them.  */
	if (irq_master_base == 0x08)
	{
		pic_reprogrammed = TRUE;

		i16_cli();

		irq_master_base = i16_find_free_vec_range();

		/* Save the old vectors in that range
		   and set them to a copy of vectors 8-15.  */
		{
			int i;

			for (i = 0; i < 8; i++)
			{
				struct far_pointer_16 hw_vec;

				i16_dos_get_int_vec(irq_master_base+i,
						    &master_save_vecs[i]);
				i16_dos_get_int_vec(0x08+i, &hw_vec);
				i16_dos_set_int_vec(irq_master_base+i, &hw_vec);
			}
		}

		/* Reprogram the PIC.  */
		i16_pic_set_master(irq_master_base);

		/* Inform the VCPI server.  */
		i16_vcpi_set_int_vecs(irq_master_base, irq_slave_base);
	}

	/* Initialize the switch-to-pmode data structure.  */
	vcpi_switch_data.phys_pdir = vcpi_pdir;
	vcpi_switch_data.lin_gdt = boot_image_pa+(vm_offset_t)&gdt_pdesc.limit;
	vcpi_switch_data.lin_idt = boot_image_pa+(vm_offset_t)&idt_pdesc.limit;
	vcpi_switch_data.tss_sel = KERNEL_TSS;
	vcpi_switch_data.entry_eip = (unsigned short)(vm_offset_t)&pmode_return;
	vcpi_switch_data.entry_cs = KERNEL_16_CS;

	/* Initialize the GDT and IDT pseudo-descriptors.  */
	gdt_pdesc.limit = sizeof(cpu[0].tables.gdt)-1;
	gdt_pdesc.linear_base = boot_image_pa + (vm_offset_t)&cpu[0].tables.gdt;
	idt_pdesc.limit = sizeof(cpu[0].tables.idt)-1;
	idt_pdesc.linear_base = boot_image_pa + (vm_offset_t)&cpu[0].tables.idt;

	/* Set the GDT to temporary settings
	   just for getting into pmode the first time.  */
	i16_gdt_init_temp();

	/* VCPI insists on loading a TSS immediately on entering pmode,
	   so initialize the KERNEL_TSS descriptor in the GDT.  */
	i16_fill_gdt_descriptor(&cpu[0], KERNEL_TSS,
				boot_image_pa + (vm_offset_t)&cpu[0].tables.tss,
				sizeof(cpu[0].tables.tss)-1,
				ACC_PL_K|ACC_TSS, 0);
	cpu[0].tables.tss.io_bit_map_offset = sizeof(cpu[0].tables.tss);

#if 0
	/* Dump the various VCPI data structures, for debugging.  */
	{
		int i;

		i16_puts("Switch data");
		i16_writehexl(switch_data.phys_pdir); i16_putchar(' ');
		i16_writehexl(switch_data.lin_gdt); i16_putchar(' ');
		i16_writehexl(switch_data.lin_idt); i16_putchar(' ');
		i16_writehexw(switch_data.ldt_sel); i16_putchar(' ');
		i16_writehexw(switch_data.tss_sel); i16_putchar(' ');
		i16_writehexl(switch_data.entry_eip); i16_putchar(' ');
		i16_writehexw(switch_data.entry_cs); i16_puts("");

		i16_puts("GDT pdesc");
		i16_writehexw(gdt_pdesc.limit); i16_putchar(' ');
		i16_writehexl(gdt_pdesc.linear_base); i16_puts("");

		i16_puts("IDT pdesc");
		i16_writehexw(idt_pdesc.limit); i16_putchar(' ');
		i16_writehexl(idt_pdesc.linear_base); i16_puts("");

		i16_puts("GDT");
		for (i = 0; i < GDTSZ; i++)
		{
			i16_writehexw(i*8); i16_putchar(' ');
			i16_writehexll(*((long long*)&cpu[0].tables.gdt[i]));
			i16_puts("");
		}
	}
#endif

	/* Switch into pmode briefly to initialize the CPU tables and such.  */
	i16_vcpi_switch_to_pmode();
	i16_do_32bit(

		/* Note that right now we can only access the first 1MB of memory,
		   because paging is enabled and that's the only memory region that's been mapped.
		   The rest of physical memory won't be mapped until VCPI_PAGING_INIT,
		   but VCPI_PAGING_INIT requires allocating memory for page tables,
		   and we can't call phys_mem_collect() to provide memory to the allocator
		   until all physical memory can be read and written.
		   To get out of this catch-22,
		   we call dos_mem_collect() beforehand here
		   to make low DOS memory available for allocation by VCPI_PAGING_INIT.
		   The call to phys_mem_collect() later will cause dos_mem_collect
		   to be called a second time, but it'll just do nothing then.  */
		dos_mem_collect();

		/* Initialize the basic CPU tables.  */
		cpu_init(&cpu[0]);

		/* Initialize the paging system.  */
		VCPI_PAGING_INIT(vcpi_pdir, (vm_offset_t)first_free_pte / 4 * PAGE_SIZE);

		/* Now that we can access all physical memory,
		   collect the remaining memory regions we discovered while in 16-bit mode
		   and add them to our free memory list.  */
		phys_mem_collect();

		/* Initialize the hardware interrupt vectors in the IDT.  */
		idt_irq_init();

		/* Now that we have an initialized LDT descriptor, start using it.  */
		KERNEL_LDT_INIT();

		/* Switch to real mode and back again once more,
		   to make sure everything's loaded properly.  */
		do_16bit(
			i16_vcpi_switch_to_real_mode();
			i16_vcpi_switch_to_pmode();
		);

		vcpi_start();
	);
}

/* Shouldn't be necessary, but just in case the end of the above function,
   containing the .code16, gets "optimized away"...  */
CODE16

void i16_vcpi_shutdown()
{
	if (pic_reprogrammed)
	{
		pic_reprogrammed = FALSE;

		i16_cli();

		i16_assert(irq_master_base >= 0x20);

		/* Reprogram the PIC.  */
		i16_pic_set_master(0x08);

		/* Inform the VCPI server.  */
		i16_vcpi_set_int_vecs(0x08, irq_slave_base);

		/* Restore the old interrupt vectors.  */
		{
			int i;

			for (i = 0; i < 8; i++)
			{
				i16_dos_set_int_vec(irq_master_base+i,
						    &master_save_vecs[i]);
			}
		}

		i16_sti();
	}

	if (ems_page_allocated)
	{
		ems_page_allocated = 0;
		asm volatile("int $0x67" : : "a" (0x4500), "d" (ems_handle));
	}
}

#endif ENABLE_VCPI