2022-09-14 16:50:44 -05:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2022-09-21 17:14:11 -05:00
|
|
|
#include "dosfs/dosfs.h"
|
2022-09-14 21:54:59 -05:00
|
|
|
#include "print.h"
|
|
|
|
#include "interrupt.h"
|
2022-09-19 16:06:01 -05:00
|
|
|
#include "tss.h"
|
2023-01-31 21:26:43 -06:00
|
|
|
#include "paging.h"
|
2023-02-02 14:52:26 -06:00
|
|
|
#include "v86defs.h"
|
2022-09-14 21:54:59 -05:00
|
|
|
|
|
|
|
typedef unsigned short word;
|
2022-09-14 16:50:44 -05:00
|
|
|
|
|
|
|
char check_apic() {
|
|
|
|
uint32_t eax, ebx, ecx, edx;
|
|
|
|
asm("cpuid" : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx) : "a"(1));
|
|
|
|
return (edx & (1 << 9)) != 0;
|
|
|
|
}
|
|
|
|
char check_sse() {
|
|
|
|
uint32_t eax, ebx, ecx, edx;
|
|
|
|
asm("cpuid" : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx) : "a"(1));
|
|
|
|
return (edx & (1 << 25)) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void enable_sse() {
|
|
|
|
asm volatile(
|
|
|
|
"mov %%cr0, %%eax\n"
|
|
|
|
"and $0xFFFB, %%ax\n"
|
|
|
|
"or $0x2, %%ax\n"
|
|
|
|
"mov %%eax, %%cr0\n"
|
|
|
|
"mov %%cr4, %%eax\n"
|
|
|
|
"or $0x600, %%ax\n"
|
|
|
|
"mov %%eax, %%cr4\n"
|
|
|
|
: : : "%eax"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-02-01 14:18:27 -06:00
|
|
|
uint32_t get_flags() {
|
2022-09-14 16:50:44 -05:00
|
|
|
uint32_t flags;
|
|
|
|
asm volatile("pushfd\npop %%eax":"=a"(flags));
|
2023-02-01 14:18:27 -06:00
|
|
|
return flags;
|
2022-09-14 16:50:44 -05:00
|
|
|
}
|
2023-02-01 14:18:27 -06:00
|
|
|
uint32_t get_cr0() {
|
2022-09-14 16:50:44 -05:00
|
|
|
uint32_t reg;
|
|
|
|
asm volatile("mov %%cr0, %%eax":"=a"(reg));
|
2023-02-01 14:18:27 -06:00
|
|
|
return reg;
|
2022-09-14 16:50:44 -05:00
|
|
|
}
|
2023-02-01 14:18:27 -06:00
|
|
|
uint32_t get_cr3() {
|
2022-09-14 16:50:44 -05:00
|
|
|
uint32_t reg;
|
|
|
|
asm volatile("mov %%cr3, %%eax":"=a"(reg));
|
2023-02-01 14:18:27 -06:00
|
|
|
return reg;
|
2022-09-14 16:50:44 -05:00
|
|
|
}
|
2023-02-01 14:18:27 -06:00
|
|
|
uint32_t get_cr4() {
|
2022-09-14 16:50:44 -05:00
|
|
|
uint32_t reg;
|
|
|
|
asm volatile("mov %%cr4, %%eax":"=a"(reg));
|
2023-02-01 14:18:27 -06:00
|
|
|
return reg;
|
2022-09-14 16:50:44 -05:00
|
|
|
}
|
|
|
|
|
2022-09-18 00:30:24 -05:00
|
|
|
extern char *jmp_usermode_test();
|
2022-09-14 16:50:44 -05:00
|
|
|
|
2023-01-31 21:26:43 -06:00
|
|
|
extern char _edata, _v86code, _ev86code, _bstart, _bend, _loadusercode, _usercode, _eusercode;
|
|
|
|
void setup_binary() {
|
|
|
|
// Put V86 code in proper place based on linker
|
|
|
|
char *s = &_edata;
|
|
|
|
char *d = &_v86code;
|
2023-02-01 20:42:05 -06:00
|
|
|
while (d < &_ev86code)
|
|
|
|
*d++ = *s++;
|
2023-01-31 21:26:43 -06:00
|
|
|
|
|
|
|
// Put Usermode code in proper place based on linker
|
|
|
|
s = &_loadusercode;
|
|
|
|
d = &_usercode;
|
|
|
|
while (d < &_eusercode)
|
|
|
|
*d++ = *s++;
|
|
|
|
|
|
|
|
// Clear BSS area
|
|
|
|
for (d = &_bstart; d < &_bend; d++)
|
|
|
|
*d = 0;
|
|
|
|
}
|
|
|
|
|
2023-02-01 20:42:05 -06:00
|
|
|
uint16_t error_screen[80*50]; // 50-line VGA screen of error content
|
|
|
|
|
|
|
|
extern uint16_t *ivt;
|
|
|
|
uint16_t ivt_bkup[0x200];
|
|
|
|
uint8_t bios_bkup[0x40000];
|
|
|
|
void backup_ivtbios() {
|
|
|
|
for (int i = 0; i < 0x200; i++)
|
|
|
|
ivt_bkup[i] = ivt[i];
|
|
|
|
for (int i = 0; i < 0x40000; i++)
|
|
|
|
bios_bkup[i] = ((uint8_t*)0xC0000)[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
// This should only be used during fault handling, as it is expensive
|
|
|
|
void ensure_v86env() {
|
|
|
|
for (int i = 0; i < 0x200; i++)
|
|
|
|
ivt[i] = ivt_bkup[i];
|
|
|
|
for (int i = 0; i < 0x40000; i++)
|
|
|
|
((uint8_t*)0xC0000)[i] = bios_bkup[i];
|
|
|
|
char *s = &_edata;
|
|
|
|
char *d = &_v86code;
|
|
|
|
while (d < &_ev86code)
|
|
|
|
*d++ = *s++;
|
|
|
|
for (int i = 0; i < 80*50; i++)
|
2023-02-02 00:50:16 -06:00
|
|
|
if (!(error_screen[i] & 0xFF00))
|
|
|
|
error_screen[i] = 0x0f00 | (error_screen[i] & 0x00FF);
|
2023-02-01 20:42:05 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void error_environment() {
|
|
|
|
ensure_v86env();
|
2023-02-02 14:52:26 -06:00
|
|
|
union V86Regs_t regs;
|
2023-02-01 20:42:05 -06:00
|
|
|
FARPTR v86_entry = i386LinearToFp(v86TextMode);
|
2023-02-02 14:52:26 -06:00
|
|
|
enter_v86(0x8000, 0xFF00, FP_SEG(v86_entry), FP_OFF(v86_entry), ®s);
|
2023-02-02 01:44:35 -06:00
|
|
|
printStr("Oh noes!!! System error! ;c Press E for a fun recovery :3", &error_screen[80]);
|
2023-02-01 20:42:05 -06:00
|
|
|
uint16_t *vga_text = ((uint16_t*)0xB8000);
|
|
|
|
for (int i = 0; i < 80*50; i++)
|
|
|
|
vga_text[i] = error_screen[i];
|
2023-02-02 01:44:35 -06:00
|
|
|
uint8_t key;
|
|
|
|
for (key = get_key(); key != 'e' && key != 'E'; key = get_key());
|
|
|
|
v86_entry = i386LinearToFp(v86TransFlag);
|
2023-02-02 14:52:26 -06:00
|
|
|
enter_v86(0x8000, 0xFF00, FP_SEG(v86_entry), FP_OFF(v86_entry), ®s);
|
2023-02-01 20:42:05 -06:00
|
|
|
}
|
|
|
|
|
2022-09-14 16:50:44 -05:00
|
|
|
/*
|
|
|
|
Real Mode Accessible (First MB)
|
2022-09-19 16:06:01 -05:00
|
|
|
00000 - 00400 IVT (1kB)
|
|
|
|
00400 - 01000 Unused (3kB)
|
2023-01-31 21:26:43 -06:00
|
|
|
01000 - 04000 Free (12kB)
|
2023-02-02 14:52:26 -06:00
|
|
|
04000 - 07C00 V86 Code (15kB)
|
|
|
|
07C00 - 08000 Boot & V86 Code (512B)
|
2023-01-31 21:26:43 -06:00
|
|
|
08000 - 20000 V86 Code (96kB)
|
|
|
|
20000 - 30000 Disk Buffer (64kB)
|
2022-09-19 19:46:26 -05:00
|
|
|
30000 - 80000 Free (320kB)
|
2022-09-19 16:06:01 -05:00
|
|
|
80000 - 90000 Real Mode Stack (64kB)
|
|
|
|
90000 - A0000 Free (64kB)
|
2023-01-31 21:26:43 -06:00
|
|
|
A0000 - C0000 VGA (128kB)
|
|
|
|
C0000 - FFFFF BIOS Area (256kB)
|
2022-09-14 16:50:44 -05:00
|
|
|
Protected Only (1MB+)
|
2023-01-31 21:26:43 -06:00
|
|
|
100000 - 200000 Kernel Code (1mB)
|
|
|
|
200000 - 200080 TSS (128B)
|
|
|
|
200080 - 202080 TSS IOMAP (8kB)
|
|
|
|
202080 - 300000 Free (~1mB)
|
2022-09-19 16:06:01 -05:00
|
|
|
300000 - 310000 Task Stack (64kB)
|
|
|
|
310000 - 320000 Interrupt Stack (64kB)
|
|
|
|
320000 - 400000 Kernel Stack (896kB)
|
2023-01-31 21:26:43 -06:00
|
|
|
400000 - 700000 Usermode Code (3mB)
|
|
|
|
700000 - 800000 Usermode Stack (1mB)
|
2022-09-14 16:50:44 -05:00
|
|
|
*/
|
|
|
|
|
2022-09-21 17:14:11 -05:00
|
|
|
void TestV86() {
|
2023-02-02 14:52:26 -06:00
|
|
|
union V86Regs_t regs;
|
|
|
|
regs.d.edi = 0x11111111;
|
|
|
|
regs.d.esi = 0x22222222;
|
|
|
|
regs.d.ebx = 0x33333333;
|
|
|
|
regs.d.edx = 0x44444444;
|
|
|
|
regs.d.ecx = 0x55555555;
|
|
|
|
regs.d.eax = 0x66666666;
|
2022-09-21 17:14:11 -05:00
|
|
|
FARPTR v86_entry = i386LinearToFp(v86Test);
|
2023-02-02 14:52:26 -06:00
|
|
|
enter_v86(0x8000, 0xFF00, FP_SEG(v86_entry), FP_OFF(v86_entry), ®s);
|
2022-09-21 17:14:11 -05:00
|
|
|
}
|
2023-02-02 00:50:16 -06:00
|
|
|
void TestUser() {
|
2022-09-21 17:14:11 -05:00
|
|
|
char *vga = jmp_usermode_test();
|
|
|
|
for (int i = 0; i < 320; i++) {
|
|
|
|
vga[i] = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void TestDiskRead() {
|
2023-02-02 14:52:26 -06:00
|
|
|
union V86Regs_t regs;
|
2023-02-01 14:18:27 -06:00
|
|
|
word *vga_text = (word *)0xb8000 + (80*5);
|
|
|
|
vga_text += printStr("Setting Text Mode... ", vga_text);
|
2023-02-02 14:52:26 -06:00
|
|
|
regs.w.ax = 3; // text mode
|
|
|
|
FARPTR v86_entry = i386LinearToFp(v86VideoInt);
|
|
|
|
enter_v86(0x8000, 0xFF00, FP_SEG(v86_entry), FP_OFF(v86_entry), ®s);
|
2023-02-01 14:18:27 -06:00
|
|
|
vga_text += printStr("Done. Starting Disk Read... ", vga_text);
|
2022-09-21 17:14:11 -05:00
|
|
|
v86_entry = i386LinearToFp(v86DiskRead);
|
2023-02-02 14:52:26 -06:00
|
|
|
enter_v86(0x8000, 0xFF00, FP_SEG(v86_entry), FP_OFF(v86_entry), ®s);
|
2023-02-01 14:18:27 -06:00
|
|
|
vga_text = (word *)0xb8000;
|
2022-09-21 17:14:11 -05:00
|
|
|
char *diskReadBuf = (char *)0x23000;
|
|
|
|
for (int i = 0; i < (80*25)/2; i++) {
|
|
|
|
printByte(diskReadBuf[i], &vga_text[i*2]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void TestFAT() {
|
|
|
|
word *vga_text = (word *)0xb8000;
|
2023-01-04 19:36:29 -06:00
|
|
|
uint8_t *diskReadBuf = (uint8_t *)0x22400;
|
2022-09-21 17:14:11 -05:00
|
|
|
for (int i = 0; i < 80*25; i++)
|
|
|
|
vga_text[i] = 0x0f00;
|
|
|
|
VOLINFO vi;
|
|
|
|
|
|
|
|
uint8_t pactive, ptype;
|
|
|
|
uint32_t pstart, psize;
|
|
|
|
pstart = DFS_GetPtnStart(0, diskReadBuf, 0, &pactive, &ptype, &psize);
|
|
|
|
vga_text = (word *)0xb8000;
|
|
|
|
vga_text += printStr("PartStart: ", vga_text);
|
|
|
|
vga_text += printDword(pstart, vga_text);
|
|
|
|
vga_text += 2;
|
|
|
|
vga_text += printStr("PartSize: ", vga_text);
|
|
|
|
vga_text += printDword(psize, vga_text);
|
|
|
|
vga_text += 2;
|
|
|
|
vga_text += printStr("PartActive: ", vga_text);
|
|
|
|
vga_text += printByte(pactive, vga_text);
|
|
|
|
vga_text += 2;
|
|
|
|
vga_text += printStr("PartType: ", vga_text);
|
|
|
|
vga_text += printByte(ptype, vga_text);
|
|
|
|
vga_text = (word *)((((((uintptr_t)vga_text)-0xb8000) - ((((uintptr_t)vga_text)-0xb8000) % 160)) + 160)+0xb8000);
|
2023-01-04 19:36:29 -06:00
|
|
|
//asm ("xchgw %bx, %bx");
|
2022-09-21 17:14:11 -05:00
|
|
|
|
|
|
|
DFS_GetVolInfo(0, diskReadBuf, pstart, &vi);
|
|
|
|
vga_text += printStr("Label: ", vga_text);
|
|
|
|
vga_text += printStr((char*)vi.label, vga_text);
|
|
|
|
vga_text += 2;
|
|
|
|
vga_text += printStr("Sec/Clus: ", vga_text);
|
|
|
|
vga_text += printByte(vi.secperclus, vga_text);
|
|
|
|
vga_text += 2;
|
|
|
|
vga_text += printStr("ResrvSec: ", vga_text);
|
|
|
|
vga_text += printWord(vi.reservedsecs, vga_text);
|
|
|
|
vga_text += 2;
|
|
|
|
vga_text += printStr("NumSec: ", vga_text);
|
|
|
|
vga_text += printDword(vi.numsecs, vga_text);
|
|
|
|
vga_text += 2;
|
|
|
|
vga_text += printStr("Sec/FAT: ", vga_text);
|
|
|
|
vga_text += printDword(vi.secperfat, vga_text);
|
|
|
|
vga_text += 2;
|
|
|
|
vga_text += printStr("FAT1@: ", vga_text);
|
|
|
|
vga_text += printDword(vi.fat1, vga_text);
|
|
|
|
vga_text += 2;
|
|
|
|
vga_text += printStr("ROOT@: ", vga_text);
|
|
|
|
vga_text += printDword(vi.rootdir, vga_text);
|
|
|
|
vga_text = (word *)((((((uintptr_t)vga_text)-0xb8000) - ((((uintptr_t)vga_text)-0xb8000) % 160)) + 160)+0xb8000);
|
2023-01-04 19:36:29 -06:00
|
|
|
//asm ("xchgw %bx, %bx");
|
2022-09-21 17:14:11 -05:00
|
|
|
|
|
|
|
vga_text += printStr("Files in root:", vga_text);
|
|
|
|
DIRINFO di;
|
2022-09-22 10:08:56 -05:00
|
|
|
di.scratch = diskReadBuf;
|
|
|
|
DFS_OpenDir(&vi, (uint8_t*)"", &di);
|
|
|
|
vga_text = (word *)((((((uintptr_t)vga_text)-0xb8000) - ((((uintptr_t)vga_text)-0xb8000) % 160)) + 160)+0xb8000);
|
2022-09-21 17:14:11 -05:00
|
|
|
DIRENT de;
|
|
|
|
while (!DFS_GetNext(&vi, &di, &de)) {
|
|
|
|
if (de.name[0]) {
|
|
|
|
for (int i = 0; i < 11 && de.name[i]; i++) {
|
2022-09-22 10:08:56 -05:00
|
|
|
if (i == 8) { *(uint8_t*)vga_text = ' '; vga_text++; } // space for 8.3
|
2022-09-21 17:14:11 -05:00
|
|
|
*(uint8_t *)vga_text = de.name[i];
|
|
|
|
vga_text++;
|
|
|
|
}
|
2023-01-04 19:36:29 -06:00
|
|
|
vga_text += printStr(" ", vga_text);
|
|
|
|
vga_text += printDec((uint32_t)de.filesize_0 + ((uint32_t)de.filesize_1 << 8) + ((uint32_t)de.filesize_2 << 16) + ((uint32_t)de.filesize_3 << 24), vga_text);
|
|
|
|
*(uint8_t*)vga_text++ = 'B';
|
2022-09-21 17:14:11 -05:00
|
|
|
vga_text = (word *)((((((uintptr_t)vga_text)-0xb8000) - ((((uintptr_t)vga_text)-0xb8000) % 160)) + 160)+0xb8000);
|
|
|
|
}
|
2023-01-04 19:36:29 -06:00
|
|
|
//asm ("xchgw %bx, %bx");
|
2022-09-21 17:14:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-14 16:50:44 -05:00
|
|
|
void start() {
|
|
|
|
word *vga_text = (word *)0xb8000;
|
|
|
|
char h[] = "LuciaOS";
|
|
|
|
for (int i = 0; i < sizeof(h); i++)
|
|
|
|
*(char *)&vga_text[i] = h[i];
|
2023-02-01 14:18:27 -06:00
|
|
|
vga_text = &vga_text[80];
|
2022-09-14 16:50:44 -05:00
|
|
|
|
|
|
|
uint32_t o;
|
|
|
|
asm("mov %%esp, %%eax" : "=a"(o) : :);
|
2023-02-01 14:18:27 -06:00
|
|
|
vga_text += printDword(o, vga_text);
|
|
|
|
vga_text++;
|
2022-09-14 16:50:44 -05:00
|
|
|
asm("mov %%ebp, %%eax" : "=a"(o) : :);
|
2023-02-01 14:18:27 -06:00
|
|
|
vga_text += printDword(o, vga_text);
|
|
|
|
vga_text++;
|
2022-09-14 16:50:44 -05:00
|
|
|
|
|
|
|
//char c[] = "APIC support: ";
|
|
|
|
//char apic;
|
|
|
|
//printByte(apic = check_apic(), (short*)&vga_text[80*3 + sizeof(c) - 1]);
|
|
|
|
//for (int i = 0; i < sizeof(c); i++)
|
|
|
|
// *(char *)&vga_text[i+(80*3)] = c[i];
|
|
|
|
//if (!apic) return;
|
|
|
|
|
|
|
|
char sse_str[] = "SSE support: ";
|
2023-02-01 14:18:27 -06:00
|
|
|
vga_text += printStr("SSE: ", vga_text);
|
|
|
|
char sse = check_sse();
|
|
|
|
if (!sse) {
|
|
|
|
*vga_text = 'N';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
vga_text += printStr("Y ", vga_text);
|
2022-09-14 16:50:44 -05:00
|
|
|
enable_sse();
|
|
|
|
|
2023-01-31 21:26:43 -06:00
|
|
|
setup_binary();
|
|
|
|
|
2022-09-16 15:50:58 -05:00
|
|
|
// edit
|
2022-09-14 16:50:44 -05:00
|
|
|
setup_interrupts();
|
2022-09-14 21:54:59 -05:00
|
|
|
setup_tss();
|
2023-01-31 21:26:43 -06:00
|
|
|
init_paging();
|
|
|
|
//print_flags();
|
2023-02-01 14:18:27 -06:00
|
|
|
vga_text += printStr("CR0:", vga_text);
|
|
|
|
vga_text += printDword(get_cr0(), vga_text);
|
|
|
|
vga_text++;
|
2023-01-31 21:26:43 -06:00
|
|
|
//print_cr3();
|
|
|
|
//print_cr4();
|
2022-09-18 00:30:24 -05:00
|
|
|
|
2023-02-01 14:18:27 -06:00
|
|
|
vga_text += printStr("V86 Test... ", vga_text);
|
2023-02-01 20:42:05 -06:00
|
|
|
//asm ("xchgw %bx, %bx");
|
2022-09-21 17:14:11 -05:00
|
|
|
TestV86(); // has int 3 wait in v86
|
2023-02-01 14:18:27 -06:00
|
|
|
vga_text = (word *)0xb8000 + (80*3);
|
2023-02-01 20:42:05 -06:00
|
|
|
backup_ivtbios();
|
|
|
|
vga_text += printStr("Done. Press 'N' for next test.", vga_text);
|
|
|
|
uint8_t key;
|
|
|
|
while ((key = get_key()) != 'N') {
|
|
|
|
*vga_text = (*vga_text & 0xFF00) | key;
|
|
|
|
vga_text++;
|
|
|
|
}
|
2023-02-02 00:50:16 -06:00
|
|
|
TestUser();
|
2022-09-21 17:14:11 -05:00
|
|
|
kbd_wait();
|
|
|
|
TestDiskRead();
|
|
|
|
kbd_wait();
|
|
|
|
TestFAT();
|
2022-09-19 19:46:26 -05:00
|
|
|
kbd_wait();
|
2023-02-01 20:42:05 -06:00
|
|
|
|
|
|
|
vga_text = &((uint16_t*)0xB8000)[80*16];
|
|
|
|
vga_text += printStr("Press ` for a flagrant system error... ", vga_text);
|
|
|
|
while ((key = get_key()) != '`') {
|
|
|
|
*vga_text = (*vga_text & 0xFF00) | key;
|
|
|
|
vga_text++;
|
|
|
|
}
|
|
|
|
// flagrant system error
|
|
|
|
*((uint8_t*)0x1000000) = 0;
|
2022-09-14 16:50:44 -05:00
|
|
|
}
|
|
|
|
|