Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bb65def53c | ||
|
|
0d970a6d25 | ||
|
|
ea159db100 | ||
|
|
e3f27812ad | ||
|
|
d78669df9c | ||
|
|
7313b01732 | ||
|
|
b2497d1ee0 | ||
|
|
30078417b6 | ||
|
|
c18cbf7be3 | ||
|
|
2d95371aa1 | ||
|
|
13b6f39e1a | ||
|
|
30aedfc97b | ||
|
|
26bb071b4d | ||
|
|
3a75e0505f | ||
|
|
a02df12924 | ||
|
|
699f53ac15 | ||
|
|
1119855215 | ||
|
|
0c82250bd2 | ||
|
|
2a66443a49 | ||
|
|
9874802ee4 | ||
|
|
44a305721e | ||
|
|
73f4d6b728 | ||
|
|
0efe8af5aa | ||
|
|
edc9b2c3ff | ||
|
|
c8a9fcf0d5 | ||
|
|
dc231741b7 | ||
|
|
fbaf9df59f | ||
|
|
58a74e4859 | ||
|
|
cc1753a75d | ||
|
|
3c8587acf5 | ||
|
|
db2814596b | ||
|
|
8cfa7bc009 | ||
|
|
d100ab53a8 | ||
|
|
35e5aa2ee5 | ||
|
|
cd9a3a7284 | ||
|
|
294500fb70 | ||
|
|
bb4404df23 | ||
|
|
aa5ca83889 | ||
|
|
f2839cdd0f | ||
|
|
31bf82e2ae | ||
|
|
4f9206af91 | ||
|
|
2eec448653 | ||
|
|
9932b92963 | ||
|
|
5d8343ed40 | ||
|
|
2fa4193517 | ||
|
|
01c73f8474 | ||
|
|
7264a31ff3 | ||
|
|
c0a00b528e | ||
|
|
9f8a4ea100 | ||
|
|
6251fee3ff | ||
|
|
f5c6a35cef | ||
|
|
e32282f6f0 | ||
|
|
2b87792c67 | ||
|
|
e480bd0ed0 | ||
|
|
e357f2ba44 | ||
|
|
f8f9b72588 | ||
|
|
d86bf47211 | ||
|
|
757f8f8c65 | ||
|
|
8303451929 | ||
|
|
1c6c922b45 | ||
|
|
7632445d43 | ||
|
|
601ea50e4b | ||
|
|
722494af44 | ||
|
|
e1f38bd5ea | ||
|
|
bcf1bd3bde | ||
|
|
f7dde241ad | ||
|
|
8638840cd8 | ||
|
|
4273c703e9 | ||
|
|
b2597fb5da | ||
|
|
291ba2bc1c | ||
|
|
71bc3543a1 | ||
|
|
a87d24d417 | ||
|
|
6fcc6da022 | ||
|
|
dc07fd0255 | ||
|
|
eb0a4b2a4d | ||
|
|
0ae3c6c96d | ||
|
|
4814b51daf | ||
|
|
14bb3adf37 | ||
|
|
cda45b205d | ||
|
|
cfd42550fc | ||
|
|
673ce8f097 | ||
|
|
ba0befa21b | ||
|
|
fa4de31aa4 | ||
|
|
3412d9ece9 | ||
|
|
e8978c73f2 | ||
|
|
f79b0098c5 | ||
|
|
10d7e7f0c6 | ||
|
|
e088f942d1 | ||
|
|
6514af8e94 | ||
|
|
602a7525d2 | ||
|
|
617b861524 | ||
|
|
2f968de7ed | ||
|
|
68e85999fb | ||
|
|
6b1445935c | ||
|
|
665d8aef80 | ||
|
|
af5ad73cf7 | ||
|
|
4c67faaad4 | ||
|
|
4fcea6a283 | ||
|
|
c958b9ffcc | ||
|
|
3d911ab191 | ||
|
|
d04c5444c7 | ||
|
|
209658ee09 | ||
|
|
f3f45445b3 | ||
|
|
459d5c36aa | ||
|
|
22c9891c4f | ||
|
|
2a832bfb8b | ||
|
|
496445e533 | ||
|
|
156642293f | ||
|
|
4e46b98efc | ||
|
|
ce3e2c55df | ||
|
|
cb24d00e5f | ||
|
|
4500f3cbca | ||
|
|
0781962e9f | ||
|
|
e25d28ebc7 | ||
|
|
93a4065dea | ||
|
|
5b017fb810 | ||
|
|
37814b4f86 | ||
|
|
238659e94b | ||
|
|
c16aa0e9c3 | ||
|
|
2935903bae | ||
|
|
5179b5881b | ||
|
|
a0c62937ff | ||
|
|
657287c160 | ||
|
|
d600e089e5 | ||
|
|
dd07a08388 | ||
|
|
63fbbc9051 | ||
|
|
3e32802862 | ||
|
|
40ff805f7d | ||
|
|
9ac69c9003 | ||
|
|
7aa28da1ef | ||
|
|
222a1efea4 | ||
|
|
7a4edf38f0 | ||
|
|
dda1b6379f | ||
|
|
98134957e7 | ||
|
|
22463e729b | ||
|
|
8de86dd647 | ||
|
|
f6fde01b06 | ||
|
|
ea6f485bd9 | ||
|
|
2ab388f2e9 | ||
|
|
cc891f6d4d | ||
|
|
f5e5c40c12 | ||
|
|
7b09c9beed | ||
|
|
103205bff2 | ||
|
|
25ee1775f0 | ||
|
|
a423cdf1b1 | ||
|
|
7c8744d1e0 | ||
|
|
4081a220ef | ||
|
|
9adc248225 | ||
|
|
676fd17bf5 | ||
|
|
b1a8f8d348 | ||
|
|
9b52ae58e8 | ||
|
|
5799f94efe | ||
|
|
400a7b22db | ||
|
|
2bba0dfca4 | ||
|
|
edba40b2c6 | ||
|
|
55604468c8 | ||
|
|
71c30fa932 | ||
|
|
b3fb599b25 | ||
|
|
d357aada75 | ||
|
|
1fef8ada29 | ||
|
|
5231930543 | ||
|
|
95b9e4558a | ||
|
|
354555d7f1 | ||
|
|
1b69810e8f | ||
|
|
64190b04cb | ||
|
|
d7b12c7896 | ||
|
|
11f922da33 | ||
|
|
59e084c234 | ||
|
|
f21ee0d149 | ||
|
|
2a7c783bba | ||
|
|
1f2a2bea48 | ||
|
|
8eedf4e267 | ||
|
|
ae49c29c75 | ||
|
|
4d88810fc1 | ||
|
|
ba9cf30e67 | ||
|
|
b9675019e7 | ||
|
|
aa9809e0ed | ||
|
|
07202b902b | ||
|
|
08501e86bb | ||
|
|
10a2d63e08 | ||
|
|
8aa01ba122 | ||
|
|
0b326e2e36 | ||
|
|
e49c27cbbc | ||
|
|
49677c8499 | ||
|
|
1e1c389caf | ||
|
|
7b531e36ea | ||
|
|
7540d4302e | ||
|
|
ba7c2996d5 | ||
|
|
707d42bec4 | ||
|
|
476ae7d6fd | ||
|
|
4a431f7d93 | ||
|
|
555ec9b627 | ||
|
|
c11361d40f | ||
|
|
c8a81b1101 | ||
|
|
41531cf4d2 | ||
|
|
edbe33bcfa | ||
|
|
747e824dae | ||
|
|
b1be5b888f | ||
|
|
dba58b2277 | ||
|
|
72b83b0c3c | ||
|
|
90711adada | ||
|
|
4f98c469b7 | ||
|
|
7d2b535df5 | ||
|
|
2f1a12be81 | ||
|
|
5ca95a5662 | ||
|
|
7471a846ec | ||
|
|
2ee920fdd2 | ||
|
|
d24eafbd63 | ||
|
|
6f94bc3ddd | ||
|
|
777820d471 | ||
|
|
b3ca9af48a | ||
|
|
43d945b5e8 | ||
|
|
bc7fd3f2fc | ||
|
|
b72c69ef1c | ||
|
|
66bd51ffb0 | ||
|
|
16143a727a | ||
|
|
63e6fd4bb5 | ||
|
|
b4a4638a82 | ||
|
|
93763917c1 | ||
|
|
743cf9ffef | ||
|
|
b9f051017c | ||
|
|
59890868c1 | ||
|
|
6b715c9051 | ||
|
|
89f7d2120f | ||
|
|
7515ebdf1c | ||
|
|
b0e59b3e26 | ||
|
|
153a8d43f1 | ||
|
|
bf9a005dbc | ||
|
|
53e2e52bbe | ||
|
|
12067b8e1a | ||
|
|
8278c4211b | ||
|
|
3bdc5bccdd | ||
|
|
f904ec2b48 | ||
|
|
bcec23ef89 | ||
|
|
d86745d91d | ||
|
|
cf5956dcd3 | ||
|
|
8498de6b62 | ||
|
|
f761d0a445 | ||
|
|
f29bd9d00c | ||
|
|
f236a64b21 | ||
|
|
1225e98276 | ||
|
|
cb31e36e90 | ||
|
|
0568a813f2 | ||
|
|
37d5621e41 | ||
|
|
98e992aad0 | ||
|
|
06242a0c9f | ||
|
|
e214a9c3de | ||
|
|
c259ae4cee | ||
|
|
b4d8707243 | ||
|
|
6b7b14d954 | ||
|
|
9e7c3ee676 | ||
|
|
2d7bf5197f | ||
|
|
7562be585f | ||
|
|
3d04eec427 | ||
|
|
5c97acba5f | ||
|
|
c3ead56582 | ||
|
|
15559b0a56 | ||
|
|
c58284761a | ||
|
|
bd99e429a9 | ||
|
|
d0acf5a718 | ||
|
|
b40151055c | ||
|
|
1e00d7a9e9 | ||
|
|
79b34477ed | ||
|
|
163ef9f79e | ||
|
|
3c549f0838 | ||
|
|
d8c85ad045 | ||
|
|
7c5af33ace | ||
|
|
6f3521c576 | ||
|
|
5a5d60abae | ||
|
|
9182a83ef2 | ||
|
|
195b7496f4 | ||
|
|
b1042fd649 | ||
|
|
6beb7da4cd | ||
|
|
f27851def9 | ||
|
|
18f1bfc88f | ||
|
|
cd6983e2cd | ||
|
|
8df1646c61 | ||
|
|
3e25ab6ae4 | ||
|
|
05cc2da2e3 | ||
|
|
a54536660b | ||
|
|
9e3d9b18ea | ||
|
|
ef01239468 | ||
|
|
372b7265f1 | ||
|
|
1bc8722098 | ||
|
|
cd2f3f9a84 | ||
|
|
08a35e40d4 | ||
|
|
a855702214 | ||
|
|
706f2c1c5d | ||
|
|
704fd15f7b | ||
|
|
f054bab5cc | ||
|
|
3fa8e4a24b | ||
|
|
a38f95a7b8 | ||
|
|
dd57a31d38 | ||
|
|
406f17c1db | ||
|
|
0be1b113aa | ||
|
|
d782b59768 | ||
|
|
c3a6139f83 | ||
|
|
3d3a433800 | ||
|
|
54451bcf5b | ||
|
|
1fc2dd34e5 | ||
|
|
cb6bd3914c | ||
|
|
56c3be9207 | ||
|
|
c6ae200a54 | ||
|
|
6b1de7c9d4 | ||
|
|
ebfeca47b5 | ||
|
|
43f4a1f4b1 | ||
|
|
0f73efbdc3 | ||
|
|
a084ab296c | ||
|
|
aa28284187 | ||
|
|
87c135b010 | ||
|
|
3d30025d3b | ||
|
|
556b0f0c4d | ||
|
|
bffd652c16 | ||
|
|
cbd427b14b | ||
|
|
fa4ff5facf | ||
|
|
db16e69d5d | ||
|
|
1d3b0a5621 | ||
|
|
d03a34ab3e | ||
|
|
494ef6de81 | ||
|
|
784009ba6c | ||
|
|
9bddf11ee4 | ||
|
|
313b86fdc7 | ||
|
|
75491dd36b | ||
|
|
c8db9fe905 | ||
|
|
d0cbaba4d1 | ||
|
|
1f22d0c900 | ||
|
|
a474368435 | ||
|
|
fb88595dec | ||
|
|
70b4db9dbb | ||
|
|
9fde638b6e | ||
|
|
b4b77dd52c | ||
|
|
79951bd0f1 | ||
|
|
41152cb4ca | ||
|
|
dba3d28bbf | ||
|
|
610f30fae9 | ||
|
|
e22b90b768 | ||
|
|
08fc9a5445 | ||
|
|
a66df19edc | ||
|
|
5bc844649b | ||
|
|
0d390465f6 | ||
|
|
04d29fdb71 | ||
|
|
e9f426894c | ||
|
|
c8810e4701 | ||
|
|
b202eca0cc | ||
|
|
05de14800b | ||
|
|
8ae84716d6 | ||
|
|
bb495eceec | ||
|
|
67b90a482f | ||
|
|
85e5b76aa3 | ||
|
|
412925a790 | ||
|
|
21afa93c4b | ||
|
|
d08d54ecbf | ||
|
|
06644a02f9 | ||
|
|
8f9881889b | ||
|
|
a91ea8d620 | ||
|
|
a9a2f6dcaf | ||
|
|
9befd553eb | ||
|
|
6053745ee9 | ||
|
|
0adc6e395c | ||
|
|
bbf212d5a2 | ||
|
|
94ed9078df | ||
|
|
e97e76da7b | ||
|
|
1f5e3507ba | ||
|
|
a9a6f6f4d4 | ||
|
|
2d009273a1 | ||
|
|
2b936758ff | ||
|
|
509009d134 | ||
|
|
0aaf5db5ad | ||
|
|
89d962e33a | ||
|
|
712337c485 | ||
|
|
761e3f0483 | ||
|
|
98abfc2af0 | ||
|
|
677551fba5 | ||
|
|
7daa1c5336 | ||
|
|
79ca453f66 | ||
|
|
0521733e20 | ||
|
|
396c5f3452 | ||
|
|
9e03bfc8b6 | ||
|
|
2b67ff6f49 | ||
|
|
daa29212d0 | ||
|
|
83a5b032f8 | ||
|
|
345ef6c9f3 | ||
|
|
e1682a2fe7 | ||
|
|
2cefd9441c | ||
|
|
da90bcc164 | ||
|
|
4689c11cc8 | ||
|
|
692cf2fc08 | ||
|
|
b0132c866e | ||
|
|
14792ef954 | ||
|
|
bb8ff09e3f | ||
|
|
dfeabc39aa | ||
|
|
14148e4d80 | ||
|
|
80774c48fe | ||
|
|
862b36b37b | ||
|
|
4638656aaf | ||
|
|
3381041a8f | ||
|
|
f09ed66826 | ||
|
|
ba5906c7a3 | ||
|
|
05183a3949 | ||
|
|
d8580a9393 | ||
|
|
50285aa33b | ||
|
|
ee63a84c0e | ||
|
|
76a243d894 | ||
|
|
6bad3f2b96 | ||
|
|
51948ce081 | ||
|
|
b40a68f581 | ||
|
|
55b7429d4e | ||
|
|
7b805f2495 | ||
|
|
2445034e43 | ||
|
|
3c531f3b7b | ||
|
|
11cc0c87f3 | ||
|
|
bad7f79ed1 | ||
|
|
78272f6bc3 | ||
|
|
396d5015d2 | ||
|
|
1bf14ddefa | ||
|
|
c0dd3b529e | ||
|
|
8a8f56decb | ||
|
|
b9c77827bd | ||
|
|
cd02211b21 | ||
|
|
a50c2a1233 | ||
|
|
07e8a0f4cf | ||
|
|
6d23c76e84 | ||
|
|
11da8cb162 | ||
|
|
1063a68a21 | ||
|
|
286b5e9ae1 | ||
|
|
d4aadbea38 | ||
|
|
dfb71cc759 | ||
|
|
872d562d6a | ||
|
|
0729754659 | ||
|
|
f3fe4ef3b8 | ||
|
|
c76725eac3 | ||
|
|
9a8d2c29cb | ||
|
|
b54b8191c4 | ||
|
|
fdf6b71a91 | ||
|
|
802a07ebfe | ||
|
|
b5ba5b63fe | ||
|
|
b4aca026d4 | ||
|
|
6d941b7b16 | ||
|
|
a982312d2c | ||
|
|
6b6e19effd | ||
|
|
38f009f2ca | ||
|
|
6236c341dd | ||
|
|
7110307896 | ||
|
|
ab576bf18f | ||
|
|
302538b8ff | ||
|
|
7f886e44e1 | ||
|
|
8d0e6de040 | ||
|
|
0b9d7fde22 | ||
|
|
48f4046e79 | ||
|
|
2cd08dee70 | ||
|
|
311b8afc83 | ||
|
|
21e3943b5e | ||
|
|
10fd7102ca | ||
|
|
7de4f94423 | ||
|
|
0a546f1467 | ||
|
|
af6087ecec | ||
|
|
c613002307 | ||
|
|
76398df125 | ||
|
|
e83408ce83 | ||
|
|
715bb9341a | ||
|
|
e82bf2ecdf | ||
|
|
528527a7ed | ||
|
|
71aaa7a598 | ||
|
|
9070a76b18 | ||
|
|
5048a93ff7 |
No files matched your search
@@ -0,0 +1,4 @@
|
|||||||
|
.rscons*
|
||||||
|
/x86_64-elf-gcc/
|
||||||
|
/build/
|
||||||
|
/qemu/
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
[submodule "uefi-d"]
|
||||||
|
path = uefi-d
|
||||||
|
url = git://github.com/kubasz/uefi-d
|
||||||
-146
@@ -1,146 +0,0 @@
|
|||||||
//Functions.c
|
|
||||||
//05/07/03 Josh Holtrop
|
|
||||||
//for HOS
|
|
||||||
//Modified: 10/30/03
|
|
||||||
|
|
||||||
//Writes a byte out to a port
|
|
||||||
inline void outportb(unsigned int port, unsigned char value) // Output a byte to a port
|
|
||||||
{
|
|
||||||
asm volatile ("outb %%al,%%dx"::"d" (port), "a" (value));
|
|
||||||
};
|
|
||||||
|
|
||||||
//Writes a word out to a port
|
|
||||||
inline void outportw(unsigned int port, unsigned int value) // Output a word to a port
|
|
||||||
{
|
|
||||||
asm volatile ("outw %%ax,%%dx"::"d" (port), "a" (value));
|
|
||||||
};
|
|
||||||
|
|
||||||
//Reads a byte from a port
|
|
||||||
inline byte inportb(unsigned short port)
|
|
||||||
{
|
|
||||||
unsigned char ret_val;
|
|
||||||
|
|
||||||
asm volatile("inb %w1,%b0"
|
|
||||||
: "=a"(ret_val)
|
|
||||||
: "d"(port));
|
|
||||||
return ret_val;
|
|
||||||
};
|
|
||||||
|
|
||||||
//Enables (SeTs) Interrupt Flag on the processor
|
|
||||||
inline void enable_ints()
|
|
||||||
{
|
|
||||||
asm("sti");
|
|
||||||
};
|
|
||||||
|
|
||||||
//Disables (CLears) Interrupt Flag on the processor
|
|
||||||
inline void disable_ints()
|
|
||||||
{
|
|
||||||
asm("cli");
|
|
||||||
};
|
|
||||||
|
|
||||||
//Re-maps the Programmable Interrupr Controllers so IRQ0->pic1 base address, IRG8->pic2 base address
|
|
||||||
void remap_pics(int pic1, int pic2)
|
|
||||||
{
|
|
||||||
byte a1, a2;
|
|
||||||
|
|
||||||
a1 = inportb(PIC1_DATA); //0x21
|
|
||||||
a2 = inportb(PIC2_DATA); //0xA1
|
|
||||||
|
|
||||||
outportb(PIC1_COMMAND, ICW1_INIT+ICW1_ICW4); //0x20, 0x10+0x01 00010001b
|
|
||||||
outportb(PIC2_COMMAND, ICW1_INIT+ICW1_ICW4); //0xA0, 0x10+0x01 00010001b
|
|
||||||
outportb(PIC1_DATA, pic1); //0x21, pic1
|
|
||||||
outportb(PIC2_DATA, pic2); //0xA1, pic2
|
|
||||||
outportb(PIC1_DATA, 4); //0x21, 0x04 00000100b
|
|
||||||
outportb(PIC2_DATA, 2); //0xA1, 0x02 00000010b
|
|
||||||
outportb(PIC1_DATA, ICW4_8086); //0x21, 0x01 00000001b
|
|
||||||
outportb(PIC2_DATA, ICW4_8086); //0xA1, 0x01 00000001b
|
|
||||||
|
|
||||||
outportb(PIC1_DATA, a1); //0x21
|
|
||||||
outportb(PIC2_DATA, a2); //0xA1
|
|
||||||
}
|
|
||||||
|
|
||||||
//Masks interrupts on first Programmable Interrupt Controller
|
|
||||||
inline void pic1_mask(byte mask)
|
|
||||||
{
|
|
||||||
outportb(PIC1_DATA, mask); //0x21, maskfield *OCW1*
|
|
||||||
}
|
|
||||||
|
|
||||||
//Masks interrupts on second Programmable Interrupt Controller
|
|
||||||
inline void pic2_mask(byte mask)
|
|
||||||
{
|
|
||||||
outportb(PIC2_DATA, mask); //0xA1, maskfield *OCW1*
|
|
||||||
}
|
|
||||||
|
|
||||||
//Restarts the computer
|
|
||||||
inline void restart()
|
|
||||||
{
|
|
||||||
byte temp;
|
|
||||||
do
|
|
||||||
{
|
|
||||||
temp = inportb(0x64);
|
|
||||||
if (temp & 1)
|
|
||||||
inportb(0x60);
|
|
||||||
} while(temp & 2);
|
|
||||||
|
|
||||||
|
|
||||||
outportb (0x64, 0xfe);
|
|
||||||
for (;;)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//Halts (freezes) the computer
|
|
||||||
inline void halt()
|
|
||||||
{
|
|
||||||
asm("cli");
|
|
||||||
asm("hlt");
|
|
||||||
while (1)
|
|
||||||
;
|
|
||||||
}
|
|
||||||
|
|
||||||
//Initializes 8253 Programmable Interrupt Timer
|
|
||||||
inline void init_timer()
|
|
||||||
{
|
|
||||||
//set timer : 2e9c = 100hz
|
|
||||||
outportb(0x43, 0x34);
|
|
||||||
outportb(0x40, 0x9c); //lsb
|
|
||||||
outportb(0x40, 0x2e); //msb
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//Invalidates the page tables to re-cache the Translation Lookaside Buffer
|
|
||||||
/*inline void invlpg()
|
|
||||||
{
|
|
||||||
asm("invlpg");
|
|
||||||
}*/
|
|
||||||
|
|
||||||
//Signals an End Of Interrupt signal to the first PIC
|
|
||||||
inline void eoi()
|
|
||||||
{
|
|
||||||
outportb(0x20, 0x20);
|
|
||||||
}
|
|
||||||
|
|
||||||
//Signals an End Of Interrupt signal to both the second and first PIC unit
|
|
||||||
inline void eoi2()
|
|
||||||
{
|
|
||||||
outportb(0xA0, 0x20);
|
|
||||||
outportb(0x20, 0x20);
|
|
||||||
}
|
|
||||||
|
|
||||||
//Returns the size of the kernel (code & data)
|
|
||||||
// - this does not include the bss section
|
|
||||||
// - this should be 4kb aligned per the linker script
|
|
||||||
// - this should be the size of kernel.bin
|
|
||||||
inline dword kernel_size()
|
|
||||||
{
|
|
||||||
return (dword)(&_bss)-(dword)(&_code);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,81 @@
|
|||||||
|
# HOS System Requirements
|
||||||
|
|
||||||
|
HOS requires:
|
||||||
|
|
||||||
|
* A 64-bit CPU.
|
||||||
|
* A UEFI system.
|
||||||
|
* 128MB of RAM.
|
||||||
|
|
||||||
|
# Building HOS
|
||||||
|
|
||||||
|
From the top level of the repository, run:
|
||||||
|
|
||||||
|
./rscons
|
||||||
|
|
||||||
|
If you get errors about missing packages, install the packages and then re-run
|
||||||
|
the build command.
|
||||||
|
|
||||||
|
# Running HOS
|
||||||
|
|
||||||
|
HOS can run on a physical machine, in VirtualBox, or in QEMU.
|
||||||
|
|
||||||
|
## Physical Machine
|
||||||
|
|
||||||
|
To install HOS to an EFI partition, follow these steps:
|
||||||
|
|
||||||
|
1. Copy build/hello/HOS.EFI to EFI partition EFI/HOS.EFI. For example, from Linux:
|
||||||
|
|
||||||
|
sudo cp build/hello/HOS.EFI /boot/efi/EFI/HOS.EFI
|
||||||
|
|
||||||
|
2. Register EFI loader for HOS (one time only) with command:
|
||||||
|
|
||||||
|
efibootmgr -c -d /dev/nvme1n1 -p 2 -l '\EFI\HOS.EFI' -L HOS
|
||||||
|
|
||||||
|
(Update the -d device parameter and -p partition parameter as appropriate)
|
||||||
|
|
||||||
|
3. Set boot order to desired boot order. Exact order will be system dependent. Example:
|
||||||
|
|
||||||
|
efibootmgr -o 2,0,6,5
|
||||||
|
|
||||||
|
At this point you should be able to boot HOS from your EFI boot selection menu.
|
||||||
|
If you use GRUB, you can optionally create a GRUB boot entry to load HOS.EFI
|
||||||
|
with an entry like this:
|
||||||
|
|
||||||
|
menuentry 'HOS' {
|
||||||
|
insmod part_gpt
|
||||||
|
insmod chain
|
||||||
|
set root='(hd0,gpt1)'
|
||||||
|
chainloader /EFI/HOS.EFI
|
||||||
|
}
|
||||||
|
|
||||||
|
Just update the root setting for the correction location to your system's EFI
|
||||||
|
partition.
|
||||||
|
|
||||||
|
## VirtualBox
|
||||||
|
|
||||||
|
To create a VM in VirtualBox:
|
||||||
|
|
||||||
|
1. Build HOS ("./rscons").
|
||||||
|
2. In VirtualBox, select Machine -> New.
|
||||||
|
3. Set name to HOS.
|
||||||
|
4. Set Type to Other.
|
||||||
|
5. Set Version to Other/Unknown (64-bit).
|
||||||
|
6. Click Next.
|
||||||
|
7. Set memory size to 512 MB.
|
||||||
|
8. Click Next.
|
||||||
|
9. Select Do not add a virtual hard disk.
|
||||||
|
10. Click Create.
|
||||||
|
11. From the HOS repository, create a VirtualBox VMDK image with "./rscons mk-vmdk".
|
||||||
|
12. Move the HOS.vmdk file generated to the HOS VirtualBox folder.
|
||||||
|
13. In VirtualBox Manager, open HOS VM settings.
|
||||||
|
14. Under System, check the Enable EFI checkbox.
|
||||||
|
15. Under System, change the Chipset to ICH9.
|
||||||
|
16. Under Storage, add a hard disk and browse to select the HOS.vmdk file.
|
||||||
|
17. To run HOS, either start the VM through VirtualBox or with "./rscons run-vb".
|
||||||
|
|
||||||
|
## QEMU
|
||||||
|
|
||||||
|
Make sure QEMU is installed (possibly the `qemu-desktop` package). Then:
|
||||||
|
|
||||||
|
1. Build HOS ("./rscons").
|
||||||
|
2. Run `./rscons run` to run HOS in QEMU.
|
||||||
+248
@@ -0,0 +1,248 @@
|
|||||||
|
require "tmpdir"
|
||||||
|
|
||||||
|
project_name "HOS"
|
||||||
|
path_prepend "x86_64-elf-gcc/bin"
|
||||||
|
|
||||||
|
configure do
|
||||||
|
rscons "x86_64-elf-gcc.rb", "-b", "#{build_dir}/x86_64-elf-gcc"
|
||||||
|
check_d_compiler "ldc2", use: "ldc2"
|
||||||
|
check_c_compiler "x86_64-w64-mingw32-gcc", use: "x86_64-w64-mingw32-gcc"
|
||||||
|
check_c_compiler "x86_64-elf-gcc", use: "x86_64-elf-gcc"
|
||||||
|
check_c_compiler
|
||||||
|
check_program "mformat", on_fail: "Install the mtools package"
|
||||||
|
check_program "parted"
|
||||||
|
check_program "pkg-config"
|
||||||
|
check_cfg package: "freetype2", on_fail: "Install libfreetype-dev", use: "freetype"
|
||||||
|
sh %w[git submodule update --init]
|
||||||
|
end
|
||||||
|
|
||||||
|
# Kernel default font size.
|
||||||
|
KFONT_SIZE = 16
|
||||||
|
|
||||||
|
# One kilobyte.
|
||||||
|
KB = 1024
|
||||||
|
|
||||||
|
# One megabyte.
|
||||||
|
MB = 1024 * 1024
|
||||||
|
|
||||||
|
class HulkBinObj < Builder
|
||||||
|
def run(options)
|
||||||
|
@cache.mkdir_p(File.dirname(@target))
|
||||||
|
File.write(@target, <<EOF)
|
||||||
|
.section ".rodata"
|
||||||
|
.balign 4096
|
||||||
|
.global _hulk_bin_start
|
||||||
|
_hulk_bin_start:
|
||||||
|
.incbin "#{@sources.first}"
|
||||||
|
.balign 4096
|
||||||
|
.global _hulk_bin_end
|
||||||
|
_hulk_bin_end:
|
||||||
|
EOF
|
||||||
|
unless @cache.up_to_date?(@target, nil, @sources, @env)
|
||||||
|
print_run_message("Creating HULK binary object <target>#{@target}<reset>", nil)
|
||||||
|
@cache.register_build(@target, nil, @sources, @env)
|
||||||
|
end
|
||||||
|
true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Create a GPT disk image with an EFI partition containing the EFI image.
|
||||||
|
class Image < Builder
|
||||||
|
def run(options)
|
||||||
|
unless @cache.up_to_date?(@target, nil, @sources, @env)
|
||||||
|
print_run_message("Creating disk image <target>#{@target}<reset>", nil)
|
||||||
|
efi_image_size = File.stat(@sources.first).size
|
||||||
|
efi_image_size_mb = (efi_image_size + MB - 1) / MB
|
||||||
|
partition_size_mb = efi_image_size_mb + 1
|
||||||
|
empty_mb = "\0".b * MB
|
||||||
|
File.binwrite(@target, empty_mb * partition_size_mb)
|
||||||
|
system(*%W[mformat -i #{@target} ::])
|
||||||
|
system(*%W[mmd -i #{@target} ::/EFI])
|
||||||
|
system(*%W[mmd -i #{@target} ::/EFI/BOOT])
|
||||||
|
system(*%W[mcopy -i #{@target} #{@sources.first} ::/EFI/BOOT/BOOTX64.EFI])
|
||||||
|
partition_contents = File.binread(@target)
|
||||||
|
disk_image = empty_mb + partition_contents + empty_mb
|
||||||
|
File.binwrite(@target, disk_image)
|
||||||
|
system(*%W[parted --script #{@target} mklabel gpt mkpart HOS fat32 1MiB #{partition_size_mb + 1}MiB])
|
||||||
|
@cache.register_build(@target, nil, @sources, @env)
|
||||||
|
end
|
||||||
|
true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class CheckThreadLocal < Builder
|
||||||
|
def run(options)
|
||||||
|
map_file = File.binread(@sources.first)
|
||||||
|
if map_file =~ /\.(tdata|tbss)\b/
|
||||||
|
$stderr.puts "Error: found thread-local data in #{@sources.first}"
|
||||||
|
false
|
||||||
|
else
|
||||||
|
true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class FontGen < Builder
|
||||||
|
def run(options)
|
||||||
|
if @command
|
||||||
|
finalize_command
|
||||||
|
else
|
||||||
|
fontgen = @vars["fontgen"]
|
||||||
|
@sources += [fontgen]
|
||||||
|
command = %W[#{fontgen} #{@sources.first} #{KFONT_SIZE} #{@target}]
|
||||||
|
standard_command("FontGen <target>#{@target}<reset>", command, {})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
fontgen_env = env "fontgen", use: "freetype" do |env|
|
||||||
|
env.Program("^/fontgen", glob("src/fontgen/**/*.c"))
|
||||||
|
end
|
||||||
|
|
||||||
|
hulk_env = env "hulk", use: %w[ldc2 x86_64-elf-gcc] do |env|
|
||||||
|
env.add_builder(FontGen)
|
||||||
|
env.add_builder(CheckThreadLocal)
|
||||||
|
env.FontGen("^/src/hulk/kfont.d", "font/Hack-Regular.ttf",
|
||||||
|
"fontgen" => fontgen_env.expand("^/fontgen"))
|
||||||
|
env["sources"] = glob("src/hulk/**/*.d")
|
||||||
|
env["sources"] << "^/src/hulk/kfont.d"
|
||||||
|
cpu_attrs = %w[
|
||||||
|
-avx
|
||||||
|
-avx2
|
||||||
|
-avx512bf16
|
||||||
|
-avx512bitalg
|
||||||
|
-avx512bw
|
||||||
|
-avx512cd
|
||||||
|
-avx512dq
|
||||||
|
-avx512f
|
||||||
|
-avx512ifma
|
||||||
|
-avx512vbmi
|
||||||
|
-avx512vbmi2
|
||||||
|
-avx512vl
|
||||||
|
-avx512vnni
|
||||||
|
-avx512vp2intersect
|
||||||
|
-avx512vpopcntdq
|
||||||
|
-sse
|
||||||
|
-sse-unaligned-mem
|
||||||
|
-sse2
|
||||||
|
-sse3
|
||||||
|
-sse4.1
|
||||||
|
-sse4.2
|
||||||
|
-sse4a
|
||||||
|
-ssse3
|
||||||
|
]
|
||||||
|
env["DFLAGS"] += %W[-g -mtriple=x86_64-unknown-elf -mattr=#{cpu_attrs.join(",")} --betterC -release -O3 --wi --enable-cross-module-inlining -code-model=kernel --disable-red-zone]
|
||||||
|
env["D_IMPORT_PATH"] += %w[src]
|
||||||
|
env["D_IMPORT_PATH"] << env.expand("^/src")
|
||||||
|
env["LD"] = "x86_64-elf-gcc"
|
||||||
|
env["LDFLAGS"] += %w[-g -nostdlib -Tsrc/hulk/hulk.ld -Wl,--gc-sections -Wl,-Map,${_TARGET}.map -Wl,--no-warn-rwx-segments]
|
||||||
|
env["LDCMD"] = %w[${LD} -o ${_TARGET} ${LDFLAGS} ${_SOURCES} ${LIBDIRPREFIX}${LIBPATH} ${LIBLINKPREFIX}${LIBS}]
|
||||||
|
env["OBJDUMP"] = "x86_64-elf-objdump"
|
||||||
|
env["OBJCOPY"] = "x86_64-elf-objcopy"
|
||||||
|
env.Program("^/hulk.elf", "${sources}")
|
||||||
|
env.produces("^/hulk.elf", "^/hulk.elf.map")
|
||||||
|
env.CheckThreadLocal(:hulk_map_check, "^/hulk.elf.map")
|
||||||
|
env.depends("^/hulk.elf", "src/hulk/hulk.ld")
|
||||||
|
env["SIZE"] = "x86_64-elf-size"
|
||||||
|
env.Size("^/hulk.elf.size", "^/hulk.elf")
|
||||||
|
env.Disassemble("^/hulk.elf.txt", "^/hulk.elf")
|
||||||
|
env.Command("^/hulk.bin", "^/hulk.elf",
|
||||||
|
"CMD" => %W[${OBJCOPY} -O binary ${_SOURCES} ${_TARGET}],
|
||||||
|
"CMD_DESC" => "Convert ELF to binary:")
|
||||||
|
env.add_build_hook do |builder|
|
||||||
|
if builder.target.end_with?(".o")
|
||||||
|
env.Disassemble("#{builder.target}.txt", builder.target)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
hello_env = env "hello", use: %w[ldc2 x86_64-w64-mingw32-gcc] do |env|
|
||||||
|
env.add_builder(Image)
|
||||||
|
env.add_builder(CheckThreadLocal)
|
||||||
|
env.add_builder(HulkBinObj)
|
||||||
|
env["sources"] = glob("src/hello/**/*.d")
|
||||||
|
env["sources"] += %w[
|
||||||
|
src/hulk/serial.d
|
||||||
|
src/hulk/writef.d
|
||||||
|
]
|
||||||
|
env["sources"] += glob("uefi-d/source/**/*.d")
|
||||||
|
env.HulkBinObj("^/hulk_bin.S", hulk_env.expand("^/hulk.bin"))
|
||||||
|
env.Object("^/hulk_bin.o", "^/hulk_bin.S")
|
||||||
|
env.depends("^/hulk_bin.o", hulk_env.expand("^/hulk.bin"))
|
||||||
|
env["sources"] << "^/hulk_bin.o"
|
||||||
|
env["DFLAGS"] += %w[-g -mtriple=x86_64-unknown-windows-coff --betterC -release -O3 --wi --enable-cross-module-inlining --disable-red-zone]
|
||||||
|
env["D_IMPORT_PATH"] += %w[src uefi-d/source]
|
||||||
|
env["LD"] = "x86_64-w64-mingw32-gcc"
|
||||||
|
env["LDFLAGS"] += %w[-g -nostdlib -Wl,-dll -shared -Wl,--subsystem,10 -e efi_main -Wl,-Map,${_TARGET}.map]
|
||||||
|
env["LDCMD"] = %w[${LD} -o ${_TARGET} ${LDFLAGS} ${_SOURCES} ${LIBDIRPREFIX}${LIBPATH} ${LIBLINKPREFIX}${LIBS}]
|
||||||
|
env["OBJDUMP"] = "x86_64-w64-mingw32-objdump"
|
||||||
|
env.Program("^/HOS.EFI", "${sources}")
|
||||||
|
env.produces("^/HOS.EFI", "^/HOS.EFI.map")
|
||||||
|
env.CheckThreadLocal(:hos_map_check, "^/HOS.EFI.map")
|
||||||
|
env["SIZE"] = "x86_64-w64-mingw32-size"
|
||||||
|
env.Size("^/HOS.size", "^/HOS.EFI")
|
||||||
|
env.Disassemble("^/HOS.txt", "^/HOS.EFI")
|
||||||
|
env.Image("^/HOS.img", "^/HOS.EFI")
|
||||||
|
end
|
||||||
|
|
||||||
|
task "run", desc: "Run HOS in QEMU" do
|
||||||
|
FileUtils.rm_rf("qemu")
|
||||||
|
FileUtils.mkdir_p("qemu")
|
||||||
|
img = hello_env.expand("^/HOS.img")
|
||||||
|
FileUtils.cp(img, "qemu")
|
||||||
|
ovmf = "OVMF.fd"
|
||||||
|
if File.exist?("/usr/share/edk2/x64/OVMF.fd")
|
||||||
|
ovmf = "/usr/share/edk2/x64/OVMF.fd"
|
||||||
|
elsif File.exist?("/usr/share/edk2/x64/OVMF.4m.fd")
|
||||||
|
ovmf = "/usr/share/edk2/x64/OVMF.4m.fd"
|
||||||
|
end
|
||||||
|
sh %W[
|
||||||
|
qemu-system-x86_64
|
||||||
|
-machine q35
|
||||||
|
-m 128M
|
||||||
|
-cpu max
|
||||||
|
-smp cpus=4
|
||||||
|
-serial file:qemu/serial.out
|
||||||
|
-bios #{ovmf}
|
||||||
|
-drive file=qemu/HOS.img,format=raw
|
||||||
|
-device qemu-xhci
|
||||||
|
-device usb-tablet]
|
||||||
|
end
|
||||||
|
|
||||||
|
# See README.md for how to set up VirtualBox for HOS.
|
||||||
|
task "mk-vmdk", desc: "Create VirtualBox VMDK virtual drive for HOS" do
|
||||||
|
path = File.expand_path(hello_env.expand("^/HOS.img"))
|
||||||
|
sectors = File.stat(path).size / 512
|
||||||
|
File.binwrite("HOS.vmdk", <<EOF)
|
||||||
|
# Disk DescriptorFile
|
||||||
|
version=1
|
||||||
|
CID=1d1c3615
|
||||||
|
parentCID=ffffffff
|
||||||
|
createType="fullDevice"
|
||||||
|
|
||||||
|
# Extent description
|
||||||
|
RW #{sectors} FLAT "#{path}" 0
|
||||||
|
|
||||||
|
# The disk Data Base
|
||||||
|
#DDB
|
||||||
|
|
||||||
|
ddb.virtualHWVersion = "4"
|
||||||
|
ddb.adapterType="ide"
|
||||||
|
ddb.geometry.cylinders="8"
|
||||||
|
ddb.geometry.heads="16"
|
||||||
|
ddb.geometry.sectors="63"
|
||||||
|
ddb.uuid.image="03783b5a-7587-4d8b-ad04-9d70f70a94c2"
|
||||||
|
ddb.uuid.parent="00000000-0000-0000-0000-000000000000"
|
||||||
|
ddb.uuid.modification="00000000-0000-0000-0000-000000000000"
|
||||||
|
ddb.uuid.parentmodification="00000000-0000-0000-0000-000000000000"
|
||||||
|
ddb.geometry.biosCylinders="8"
|
||||||
|
ddb.geometry.biosHeads="16"
|
||||||
|
ddb.geometry.biosSectors="63"
|
||||||
|
EOF
|
||||||
|
#sh %W[VBoxManage createmedium disk --filename=HOS.vmdk --variant=RawDisk --format=VMDK --property RawDrive=#{File.expand_path(hello_env.expand("^/HOS.img"))}]
|
||||||
|
end
|
||||||
|
|
||||||
|
# See README.md for how to set up VirtualBox for HOS.
|
||||||
|
task "run-vb", desc: "Run HOS in VirtualBox" do
|
||||||
|
sh %W[VBoxManage startvm HOS]
|
||||||
|
end
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
; asmfuncs.asm
|
|
||||||
; Josh Holtrop
|
|
||||||
; 10/23/03
|
|
||||||
|
|
||||||
|
|
||||||
;stores the parameter to the CR0 register
|
|
||||||
;extern dword write_cr0(dword cr0);
|
|
||||||
[global _write_cr0]
|
|
||||||
_write_cr0:
|
|
||||||
push ebp
|
|
||||||
mov ebp, esp
|
|
||||||
mov eax, [ebp+8]
|
|
||||||
mov cr0, eax
|
|
||||||
pop ebp
|
|
||||||
ret
|
|
||||||
|
|
||||||
;returns the value in the CR0 register
|
|
||||||
;extern dword read_cr0();
|
|
||||||
[global _read_cr0]
|
|
||||||
_read_cr0:
|
|
||||||
mov eax, cr0;
|
|
||||||
ret
|
|
||||||
|
|
||||||
;stores the parameter to the CR3 register
|
|
||||||
;extern dword write_cr3(dword cr3);
|
|
||||||
[global _write_cr3]
|
|
||||||
_write_cr3:
|
|
||||||
push ebp
|
|
||||||
mov ebp, esp
|
|
||||||
mov eax, [ebp+8]
|
|
||||||
mov cr3, eax
|
|
||||||
pop ebp
|
|
||||||
ret
|
|
||||||
|
|
||||||
;returns the value in the CR3 register
|
|
||||||
;extern dword read_cr3();
|
|
||||||
[global _read_cr3]
|
|
||||||
_read_cr3:
|
|
||||||
mov eax, cr3;
|
|
||||||
ret
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-19
@@ -1,19 +0,0 @@
|
|||||||
@echo off
|
|
||||||
echo Backing up to .\Backup\%1
|
|
||||||
|
|
||||||
mkdir .\Backup\%1
|
|
||||||
mkdir .\Backup\%1\lib
|
|
||||||
|
|
||||||
copy *.h .\Backup\%1
|
|
||||||
copy *.c .\Backup\%1
|
|
||||||
copy *.inc .\Backup\%1
|
|
||||||
copy *.asm .\Backup\%1
|
|
||||||
copy *.ld .\Backup\%1
|
|
||||||
copy *.bat .\Backup\%1
|
|
||||||
|
|
||||||
copy .\lib\*.h .\Backup\%1\lib
|
|
||||||
copy .\lib\*.c .\Backup\%1\lib
|
|
||||||
copy .\lib\*.inc .\Backup\%1\lib
|
|
||||||
copy .\lib\*.asm .\Backup\%1\lib
|
|
||||||
copy .\lib\*.ld .\Backup\%1\lib
|
|
||||||
copy .\lib\*.bat .\Backup\%1\lib
|
|
||||||
-26
@@ -1,26 +0,0 @@
|
|||||||
|
|
||||||
%define VERSION "0.12" ;HOS version
|
|
||||||
|
|
||||||
%define BOOT_FAT_SEG 0x07E0 ;right after boot sector
|
|
||||||
%define BOOT_ROOT_SEG 0x0900 ;right after FAT
|
|
||||||
%define BOOT_KERNEL_SEG 0x0AC0 ;right after ROOT_DIR
|
|
||||||
%define BOOT_STAGE2_SEG 0x0B00 ;right after KERNEL_SEG
|
|
||||||
%define BOOT_STAGE2_ADD 0xB000 ;address of stage2 to jump to, org at
|
|
||||||
%define BOOT_KERNEL_ADD 0x104000 ;final pmode kernel destination - physical
|
|
||||||
%define BOOT_RD_ADD 0x200000 ;2mb for ram disk
|
|
||||||
|
|
||||||
%define BOOT_DATA_SEG 0x9000 ;data gathered by stage2 loader goes here
|
|
||||||
|
|
||||||
%define BOOT_HASRD 0x0000 ;1
|
|
||||||
%define BOOT_VESA 0x0002 ;2 - 0 for console, otherwise VESA mode
|
|
||||||
%define BOOT_VESA_OEM 0x0004 ;258 - null-terminated OEM identification string
|
|
||||||
%define BOOT_VESA_VBE 0x0106 ;512 - copy of VESA VBEInfoBlock
|
|
||||||
%define BOOT_VESA_INFO 0x0306 ;256 - copy of VESA ModeInfoBlock for selected mode
|
|
||||||
%define BOOT_MEMENTRIES 0x040A ;4 - dword = number of memmap entries
|
|
||||||
%define BOOT_MEMMAP 0x2000 ;? - memory map information
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
%define BOOT_DRIVE 0x7C24 ;1 - boot drive
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
gcc -ffreestanding -fno-builtin -nostdlib -nodefaultlibs -c kernel.c -o kernel.o
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
rem rawrite -f stage1.bin -d a -n
|
|
||||||
partcopy stage1.bin 0 200 -f0
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
//fdc.c
|
|
||||||
//Author: Josh Holtrop
|
|
||||||
//Date: 10/30/03
|
|
||||||
|
|
||||||
|
|
||||||
inline void fdc_sendDOR(byte dor)
|
|
||||||
{
|
|
||||||
outportb(FDC_DOR, dor);
|
|
||||||
}
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
//fdc.h
|
|
||||||
//Author: Josh Holtrop
|
|
||||||
//Date: 10/30/03
|
|
||||||
|
|
||||||
|
|
||||||
#define FDC_DOR 0x3f2
|
|
||||||
#define FDC_MSR 0x3f4
|
|
||||||
|
|
||||||
|
|
||||||
inline void fdc_sendDOR(byte dor);
|
|
||||||
|
|
||||||
|
|
||||||
Binary file not shown.
-32
@@ -1,32 +0,0 @@
|
|||||||
//functions.h
|
|
||||||
//05/07/03 Josh Holtrop
|
|
||||||
//for HOS
|
|
||||||
//Modified: 10/30/03
|
|
||||||
|
|
||||||
extern dword _code;
|
|
||||||
extern dword _bss;
|
|
||||||
extern dword _end;
|
|
||||||
|
|
||||||
inline void outportb(unsigned int port, unsigned char value);
|
|
||||||
inline void outportw(unsigned int port, unsigned int value);
|
|
||||||
inline byte inportb(unsigned short port);
|
|
||||||
inline void enable_ints();
|
|
||||||
inline void disable_ints();
|
|
||||||
inline void restart();
|
|
||||||
inline void halt();
|
|
||||||
void remap_pics(int pic1, int pic2);
|
|
||||||
inline void pic1_mask(byte mask);
|
|
||||||
inline void pic2_mask(byte mask);
|
|
||||||
inline void eoi();
|
|
||||||
inline void eoi2();
|
|
||||||
inline dword kernel_size();
|
|
||||||
inline void init_timer();
|
|
||||||
//inline void invlpg();
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,71 +0,0 @@
|
|||||||
;gdt.inc
|
|
||||||
;Author: Josh Holtrop
|
|
||||||
;for HOS
|
|
||||||
;Modified: 10/30/03
|
|
||||||
|
|
||||||
gdtr:
|
|
||||||
dw gdt_end-gdt-1
|
|
||||||
dd GDT
|
|
||||||
gdt:
|
|
||||||
dd 0
|
|
||||||
dd 0
|
|
||||||
KERNEL_CODE equ $-gdt
|
|
||||||
dw 0xffff ;limit 15:0
|
|
||||||
dw 0x0000 ;base 15:0
|
|
||||||
db 0x00 ;base 23:16
|
|
||||||
db 0x9A ;access ([P][DPL][1][Executable][Direction/Conforming][Writable/Readable][A])
|
|
||||||
db 0xCF ;flags ([G][D/B][0][0]) / limit 19:16
|
|
||||||
db 0x00 ;base 31:24
|
|
||||||
KERNEL_DATA equ $-gdt
|
|
||||||
dw 0xffff ;limit 15:0
|
|
||||||
dw 0x0000 ;base 15:0
|
|
||||||
db 0x00 ;base 23:16
|
|
||||||
db 0x92 ;access ([P][DPL][1][Executable][Direction/Conforming][Writable/Readable][A])
|
|
||||||
db 0xCF ;flags ([G][D/B][0][0]) / limit 19:16
|
|
||||||
db 0x00 ;base 31:24
|
|
||||||
VESA_CODE equ $-gdt
|
|
||||||
dw 0xffff ;limit 15:0
|
|
||||||
dw 0x0000 ;base 15:0
|
|
||||||
db 0x00 ;base 23:16
|
|
||||||
db 0x9A ;access ([P][DPL][1][Executable][Direction/Conforming][Writable/Readable][A])
|
|
||||||
db 0x40 ;flags ([G][D/B][0][0]) / limit 19:16
|
|
||||||
db 0x00 ;base 31:24
|
|
||||||
VESA_DATA equ $-gdt
|
|
||||||
dw 0xffff ;limit 15:0
|
|
||||||
dw 0x0000 ;base 15:0
|
|
||||||
db 0x00 ;base 23:16
|
|
||||||
db 0x92 ;access ([P][DPL][1][Executable][Direction/Conforming][Writable/Readable][A])
|
|
||||||
db 0x40 ;flags ([G][D/B][0][0]) / limit 19:16
|
|
||||||
db 0x00 ;base 31:24
|
|
||||||
VIDEO_TEXT equ $-gdt
|
|
||||||
dw 0x7FFF ;limit 15:0
|
|
||||||
dw 0x8000 ;base 15:0
|
|
||||||
db 0x0B ;base 23:16
|
|
||||||
db 0x92 ;access ([P][DPL][1][Executable][Direction/Conforming][Writable/Readable][A])
|
|
||||||
db 0x40 ;flags ([G][D/B][0][0]) / limit 19:16
|
|
||||||
db 0x00 ;base 31:24
|
|
||||||
VIDEO_GRAPHICS equ $-gdt
|
|
||||||
dw 0xFFFF ;limit 15:0
|
|
||||||
dw 0x0000 ;base 15:0
|
|
||||||
db 0x0A ;base 23:16
|
|
||||||
db 0x92 ;access ([P][DPL][1][Executable][Direction/Conforming][Writable/Readable][A])
|
|
||||||
db 0x40 ;flags ([G][D/B][0][0]) / limit 19:16
|
|
||||||
db 0x00 ;base 31:24
|
|
||||||
USER_CODE equ $-gdt
|
|
||||||
dw 0xffff ;limit 15:0
|
|
||||||
dw 0x0000 ;base 15:0
|
|
||||||
db 0x00 ;base 23:16
|
|
||||||
db 0xFA ;access ([P][DPL][1][Executable][Direction/Conforming][Writable/Readable][A])
|
|
||||||
db 0xCF ;flags ([G][D/B][0][0]) / limit 19:16
|
|
||||||
db 0x00 ;base 31:24
|
|
||||||
USER_DATA equ $-gdt
|
|
||||||
dw 0xffff ;limit 15:0
|
|
||||||
dw 0x0000 ;base 15:0
|
|
||||||
db 0x00 ;base 23:16
|
|
||||||
db 0xF2 ;access ([P][DPL][1][Executable][Direction/Conforming][Writable/Readable][A])
|
|
||||||
db 0xCF ;flags ([G][D/B][0][0]) / limit 19:16
|
|
||||||
db 0x00 ;base 31:24
|
|
||||||
gdt_end:
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
partcopy -f0 0 168000 flop.img
|
|
||||||
@@ -1,90 +0,0 @@
|
|||||||
;idt.inc
|
|
||||||
;Author: Josh Holtrop
|
|
||||||
;for HOS
|
|
||||||
;Modified: 10/30/03
|
|
||||||
|
|
||||||
idtr:
|
|
||||||
dw 50*8-1 ;size of idt
|
|
||||||
dd IDT ;address of idt
|
|
||||||
|
|
||||||
|
|
||||||
%macro isr_label 1
|
|
||||||
isr_%1:
|
|
||||||
mov eax, %1
|
|
||||||
jmp isr_main
|
|
||||||
%endmacro
|
|
||||||
|
|
||||||
isr_label 0
|
|
||||||
isr_label 1
|
|
||||||
isr_label 2
|
|
||||||
isr_label 3
|
|
||||||
isr_label 4
|
|
||||||
isr_label 5
|
|
||||||
isr_label 6
|
|
||||||
isr_label 7
|
|
||||||
isr_label 8
|
|
||||||
isr_label 9
|
|
||||||
isr_label 10
|
|
||||||
isr_label 11
|
|
||||||
isr_label 12
|
|
||||||
isr_label 13
|
|
||||||
isr_label 14
|
|
||||||
isr_label 15
|
|
||||||
isr_label 16
|
|
||||||
isr_label 17
|
|
||||||
isr_label 18
|
|
||||||
isr_label 19
|
|
||||||
isr_label 20
|
|
||||||
isr_label 21
|
|
||||||
isr_label 22
|
|
||||||
isr_label 23
|
|
||||||
isr_label 24
|
|
||||||
isr_label 25
|
|
||||||
isr_label 26
|
|
||||||
isr_label 27
|
|
||||||
isr_label 28
|
|
||||||
isr_label 29
|
|
||||||
isr_label 30
|
|
||||||
isr_label 31
|
|
||||||
isr_label 32
|
|
||||||
isr_label 33
|
|
||||||
isr_label 34
|
|
||||||
isr_label 35
|
|
||||||
isr_label 36
|
|
||||||
isr_label 37
|
|
||||||
isr_label 38
|
|
||||||
isr_label 39
|
|
||||||
isr_label 40
|
|
||||||
isr_label 41
|
|
||||||
isr_label 42
|
|
||||||
isr_label 43
|
|
||||||
isr_label 44
|
|
||||||
isr_label 45
|
|
||||||
isr_label 46
|
|
||||||
isr_label 47
|
|
||||||
isr_label 48
|
|
||||||
isr_label 49
|
|
||||||
|
|
||||||
isr_main:
|
|
||||||
pusha
|
|
||||||
push ds
|
|
||||||
push es
|
|
||||||
|
|
||||||
push eax
|
|
||||||
|
|
||||||
call _isr
|
|
||||||
|
|
||||||
pop eax
|
|
||||||
|
|
||||||
pop es
|
|
||||||
pop ds
|
|
||||||
popa
|
|
||||||
|
|
||||||
iret
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-51
@@ -1,51 +0,0 @@
|
|||||||
//k_defines.h
|
|
||||||
//03/17/03 Josh Holtrop
|
|
||||||
//Modified: 10/30/03
|
|
||||||
|
|
||||||
#define PIC1 0x20
|
|
||||||
#define PIC2 0xA0
|
|
||||||
#define PIC1_COMMAND PIC1
|
|
||||||
#define PIC1_DATA (PIC1+1)
|
|
||||||
#define PIC2_COMMAND PIC2
|
|
||||||
#define PIC2_DATA (PIC2+1)
|
|
||||||
#define PIC_EOI 0x20
|
|
||||||
|
|
||||||
#define ICW1_ICW4 0x01 /* ICW4 (not) needed */
|
|
||||||
#define ICW1_SINGLE 0x02 /* Single (cascade) mode */
|
|
||||||
#define ICW1_INTERVAL4 0x04 /* Call address interval 4 (8) */
|
|
||||||
#define ICW1_LEVEL 0x08 /* Level triggered (edge) mode */
|
|
||||||
#define ICW1_INIT 0x10 /* Initialization - required! */
|
|
||||||
|
|
||||||
#define ICW4_8086 0x01 /* 8086/88 (MCS-80/85) mode */
|
|
||||||
#define ICW4_AUTO 0x02 /* Auto (normal) EOI */
|
|
||||||
#define ICW4_BUF_SLAVE 0x08 /* Buffered mode/slave */
|
|
||||||
#define ICW4_BUF_MASTER 0x0C /* Buffered mode/master */
|
|
||||||
#define ICW4_SFNM 0x10 /* Special fully nested (not) */
|
|
||||||
|
|
||||||
#define BLUE_TXT 0x01
|
|
||||||
#define RED_TXT 0x04
|
|
||||||
#define WHITE_TXT 0x07
|
|
||||||
#define CYAN_TXT 0x0B
|
|
||||||
#define YELLOW_TXT 0x0E
|
|
||||||
#define BRWHITE_TXT 0x0F
|
|
||||||
|
|
||||||
#define FREERAM_START 0x268000
|
|
||||||
#define MAX_RAM 0x40000000 //1gb maximum RAM supported
|
|
||||||
|
|
||||||
#define PID_KERNEL 0x02 //kernel's PID
|
|
||||||
#define PID_VMM 0x03
|
|
||||||
#define PID_USER 0x10000 //user processes' start PID
|
|
||||||
|
|
||||||
typedef unsigned char byte;
|
|
||||||
typedef unsigned short word;
|
|
||||||
typedef unsigned int dword;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
dword lowdword;
|
|
||||||
dword highdword;
|
|
||||||
} __attribute__((packed)) qword;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-67
@@ -1,67 +0,0 @@
|
|||||||
;kernel.asm
|
|
||||||
;Author: Josh Holtrop
|
|
||||||
;Modified: 10/30/03
|
|
||||||
|
|
||||||
%include "bootdef.inc"
|
|
||||||
|
|
||||||
%define GDT 0x100000
|
|
||||||
%define IDT 0x102000
|
|
||||||
|
|
||||||
[global start]
|
|
||||||
[extern _isr]
|
|
||||||
[extern _k_init]
|
|
||||||
|
|
||||||
bits 32
|
|
||||||
|
|
||||||
;This is where the kernel begins execution
|
|
||||||
start:
|
|
||||||
cli ;if they weren't already off
|
|
||||||
mov edi, GDT
|
|
||||||
mov esi, gdt
|
|
||||||
mov ecx, gdt_end-gdt
|
|
||||||
copy_gdt:
|
|
||||||
lodsb
|
|
||||||
stosb
|
|
||||||
loop copy_gdt
|
|
||||||
|
|
||||||
mov edi, IDT ;destination
|
|
||||||
mov esi, isr_0 ;address of isr0
|
|
||||||
mov edx, isr_1-isr_0 ;distance between isr labels
|
|
||||||
mov ecx, 50 ;number of isrlabels
|
|
||||||
fill_idt:
|
|
||||||
mov ebx, esi
|
|
||||||
mov ax, si
|
|
||||||
stosw ;0 offset 15:0
|
|
||||||
mov ax, KERNEL_CODE
|
|
||||||
stosw ;2 selector 15:0
|
|
||||||
mov ax, 0x8E00
|
|
||||||
stosw ;4 [P][DPL][0][TYPE][0][0][0][0][0][0][0][0]
|
|
||||||
shr esi, 16
|
|
||||||
mov ax, si
|
|
||||||
stosw ;6 offset 31:16
|
|
||||||
mov esi, ebx
|
|
||||||
add esi, edx
|
|
||||||
loop fill_idt
|
|
||||||
mov word [IDT+0x30*8+4], 0xEE00 ;interrupt 0x30 has user priviledges
|
|
||||||
|
|
||||||
lgdt [gdtr] ;load gdt
|
|
||||||
jmp KERNEL_CODE:newgdtcontinue
|
|
||||||
newgdtcontinue:
|
|
||||||
mov ax, KERNEL_DATA
|
|
||||||
mov es, ax
|
|
||||||
mov ds, ax
|
|
||||||
mov gs, ax
|
|
||||||
mov fs, ax
|
|
||||||
mov ss, ax
|
|
||||||
mov esp, 0x1ffffc ;stack just under 2mb, moves downward
|
|
||||||
lidt [idtr] ;load idt
|
|
||||||
|
|
||||||
call _k_init
|
|
||||||
haltit:
|
|
||||||
hlt ;halt processor when k_init is done
|
|
||||||
jmp haltit ;shouldn't get here...
|
|
||||||
|
|
||||||
%include "gdt.inc"
|
|
||||||
%include "idt.inc"
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,114 +0,0 @@
|
|||||||
//kernel.c
|
|
||||||
//08/13/03 Josh Holtrop
|
|
||||||
//Holtrop's Operating System
|
|
||||||
//Version: 0.12
|
|
||||||
//Modified: 11/12/03
|
|
||||||
|
|
||||||
#include "k_defines.h" //#DEFINE's for kernel
|
|
||||||
|
|
||||||
#include "lib/string.h" //library string functions
|
|
||||||
#include "lib/io.h" //library input/output functions
|
|
||||||
|
|
||||||
#include "functions.h" //general functions
|
|
||||||
#include "video.h" //video functions
|
|
||||||
#include "mm.h" //physical memory management functions
|
|
||||||
#include "vmm.h" //virtual memory management & paging functions
|
|
||||||
#include "keyboard.h" //generic keyboard driver & functions
|
|
||||||
#include "mouse.h" //generic ps/2 mouse driver & functions
|
|
||||||
#include "fdc.h" //Floppy Disk Controller functions
|
|
||||||
#include "stdfont.h" //Standard font bitmask array
|
|
||||||
|
|
||||||
void isr(dword num);
|
|
||||||
void k_init();
|
|
||||||
extern dword write_cr0(dword cr0);
|
|
||||||
extern dword read_cr0();
|
|
||||||
extern dword write_cr3(dword cr3);
|
|
||||||
extern dword read_cr3();
|
|
||||||
|
|
||||||
#include "fdc.c"
|
|
||||||
#include "mouse.c"
|
|
||||||
#include "keyboard.c"
|
|
||||||
#include "mm.c"
|
|
||||||
#include "vmm.c"
|
|
||||||
#include "functions.c"
|
|
||||||
#include "video.c"
|
|
||||||
|
|
||||||
dword timer = 0;
|
|
||||||
dword *videoMode;
|
|
||||||
|
|
||||||
//Main kernel initialization method
|
|
||||||
void k_init()
|
|
||||||
{
|
|
||||||
// ===== Initialization
|
|
||||||
fdc_sendDOR(0x0C); //turn off floppy motor!!
|
|
||||||
console_cls();
|
|
||||||
remap_pics(0x20, 0x28);
|
|
||||||
init_timer();
|
|
||||||
mm_init();
|
|
||||||
videoMode = (dword *)0x90002;
|
|
||||||
if (*videoMode)
|
|
||||||
video_init((ModeInfoBlock *) 0x90306);
|
|
||||||
vmm_init();
|
|
||||||
mouse_init();
|
|
||||||
vmm_enable_paging();
|
|
||||||
pic1_mask(0); //unmask IRQ's 0-7
|
|
||||||
pic2_mask(0); //unmask IRQ's 8-15
|
|
||||||
enable_ints();
|
|
||||||
kbd_resetLEDs(); //after enabling interrupts!!
|
|
||||||
|
|
||||||
char *bmpptr = (char *)0x108000+54;
|
|
||||||
int a = 0;
|
|
||||||
int x = 0;
|
|
||||||
int y = 479;
|
|
||||||
for (a=0; a < 640*480; a++)
|
|
||||||
{
|
|
||||||
video_pset(x, y, *(dword *)bmpptr);
|
|
||||||
bmpptr += 3;
|
|
||||||
x++;
|
|
||||||
if (x == 640)
|
|
||||||
{
|
|
||||||
x = 0;
|
|
||||||
y--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("HOS 0.12 - Kernel Size: %d kb\n", kernel_size()/1024);
|
|
||||||
printf("Memory available to OS: %d MB (Bytes: %d)\n", mm_totalmem/0x100000, mm_totalmem);
|
|
||||||
printf("Free memory: %d bytes\n", mm_freemem());
|
|
||||||
|
|
||||||
dword key = 0;
|
|
||||||
for (;;)
|
|
||||||
{
|
|
||||||
key = kbdWaitKey();
|
|
||||||
if ((key & 0xFF) > 2) //key is not a control key
|
|
||||||
putc(key);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// main Interrupt Service Routine - handles all interrupts unless caught by kernel.asm
|
|
||||||
void isr(dword num)
|
|
||||||
{
|
|
||||||
switch(num)
|
|
||||||
{
|
|
||||||
case 0x20: // IRQ0 - timer interrupt
|
|
||||||
timer++;
|
|
||||||
(*(byte *)(0xb8000))++;
|
|
||||||
eoi();
|
|
||||||
break;
|
|
||||||
case 0x21: // IRQ1 - keyboard interrupt
|
|
||||||
isr_keyboard(); //isr_keybard() takes care of calling eoi()
|
|
||||||
break;
|
|
||||||
case 0x2C: // IRQ12 - PS/2 mouse
|
|
||||||
isr_mouse();
|
|
||||||
eoi2();
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
printf("Interrupt %d (0x%x) Unhandled!!\n", num, num);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-254
@@ -1,254 +0,0 @@
|
|||||||
//Keyboard.c
|
|
||||||
// Created: 04/17/03 Josh Holtrop
|
|
||||||
// Modified: 10/30/03
|
|
||||||
//for HOS
|
|
||||||
|
|
||||||
#define KBD_BUFFER_LENGTH 64
|
|
||||||
|
|
||||||
byte kbdFlags = 0; //holds current keyboard flags - caps/num/scroll/shift/ctrl/alt
|
|
||||||
byte kbdAscii = 0; //holds ASCII value of a key pressed
|
|
||||||
byte kbdScan = 0; //holds the keyboard scan code of a key pressed
|
|
||||||
|
|
||||||
dword kbdBuffer[KBD_BUFFER_LENGTH]; //a buffer for all keypresses
|
|
||||||
int kbdBufferStart = 0; //position of next key in buffer
|
|
||||||
int kbdBufferLen = 0; //number of keys left in the buffer
|
|
||||||
byte kbdExt = 0; //# of extended key codes left to input
|
|
||||||
byte kbdExt2 = 0; //# of 2nd-set-extended key codes left to input
|
|
||||||
byte ackReason = 0; //used to record the reason why we would get an acknowledge byte (0xFA)
|
|
||||||
|
|
||||||
|
|
||||||
//these arrays convert a keyboard scan code to an ASCII character value
|
|
||||||
//nul esc bksp tab lctl lsft rsft lalt caps F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 numScrlNumPad------- unknown---- F11 F12 unknown....
|
|
||||||
const byte SCAN2ASCII[128] = "\000\0331234567890-=\010\011qwertyuiop[]\n\001asdfghjkl;'`\001\\zxcvbnm,./\001*\001 \001\001\001\001\001\001\001\001\001\001\001\001\001\001\001\001-\001\001\001+\001\001\001\001\001\002\002\002\001\001\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002";
|
|
||||||
const byte SCAN2ASCIISHIFT[128] = "\000\033!@#$%^&*()_+\010\011QWERTYUIOP{}\n\001ASDFGHJKL:\"~\001|ZXCVBNM<>?\001*\001 \001\001\001\001\001\001\001\001\001\001\001\001\001789-456+1230.\002\002\002\001\001\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002";
|
|
||||||
|
|
||||||
//====FUNCTIONS:
|
|
||||||
// The Keyboard Interrupt Service Routine
|
|
||||||
void isr_keyboard()
|
|
||||||
{
|
|
||||||
kbdScan = inportb(0x60);
|
|
||||||
//printf("\nKEYBOARD INTERRUPT: 0x%x", kbdScan);
|
|
||||||
byte inState = inportb(0x61);
|
|
||||||
outportb(0x61, inState|0x80);
|
|
||||||
outportb(0x61, inState);
|
|
||||||
|
|
||||||
//printf("IRQ 1: %x\n", kbdScan);
|
|
||||||
|
|
||||||
if (kbdScan == 0xFA) //250 //ACKnowledge
|
|
||||||
{
|
|
||||||
//printf("KBD_ACK 0x%x!\n", ackReason);
|
|
||||||
if (ackReason == 0xED) //reset LEDs
|
|
||||||
{
|
|
||||||
outportb(0x60, (kbdFlags & 0x07));
|
|
||||||
}
|
|
||||||
ackReason = 0;
|
|
||||||
}
|
|
||||||
if (kbdScan == 224) //extended key
|
|
||||||
{
|
|
||||||
kbdExt = 1;
|
|
||||||
eoi();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (kbdScan == 225) //2nd-set-extended key
|
|
||||||
{
|
|
||||||
kbdExt2 = 2;
|
|
||||||
eoi();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
//====handle control keys::
|
|
||||||
kbdAscii = 2;
|
|
||||||
switch (kbdScan) //control keys
|
|
||||||
{
|
|
||||||
case KBD_SCAN_LSHIFT:
|
|
||||||
kbdFlags |= KBD_SHIFT;
|
|
||||||
kbdAscii = 1;
|
|
||||||
break;
|
|
||||||
case KBD_SCAN_RSHIFT:
|
|
||||||
kbdFlags |= KBD_SHIFT;
|
|
||||||
kbdAscii = 1;
|
|
||||||
break;
|
|
||||||
case KBD_SCAN_LCTRL:
|
|
||||||
kbdFlags |= KBD_CTRL;
|
|
||||||
kbdAscii = 1;
|
|
||||||
break;
|
|
||||||
case KBD_SCAN_LALT:
|
|
||||||
kbdFlags |= KBD_ALT;
|
|
||||||
kbdAscii = 1;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case KBD_SCAN_LSHIFT + KBD_SCAN_RELEASED:
|
|
||||||
kbdFlags &= (KBD_SHIFT ^ 0xFF);
|
|
||||||
kbdAscii = 1;
|
|
||||||
break;
|
|
||||||
case KBD_SCAN_RSHIFT + KBD_SCAN_RELEASED:
|
|
||||||
kbdFlags &= (KBD_SHIFT ^ 0xFF);
|
|
||||||
kbdAscii = 1;
|
|
||||||
break;
|
|
||||||
case KBD_SCAN_LCTRL + KBD_SCAN_RELEASED:
|
|
||||||
kbdFlags &= (KBD_CTRL ^ 0xFF);
|
|
||||||
kbdAscii = 1;
|
|
||||||
break;
|
|
||||||
case KBD_SCAN_LALT + KBD_SCAN_RELEASED:
|
|
||||||
kbdFlags &= (KBD_ALT ^ 0xFF);
|
|
||||||
kbdAscii = 1;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case KBD_SCAN_CAPS+KBD_SCAN_RELEASED:
|
|
||||||
kbdFlags ^= KBD_CAPS;
|
|
||||||
kbdAscii = 1;
|
|
||||||
kbd_resetLEDs(); //update LEDs
|
|
||||||
break;
|
|
||||||
case KBD_SCAN_SCROLL+KBD_SCAN_RELEASED:
|
|
||||||
kbdFlags ^= KBD_SCROLL;
|
|
||||||
kbdAscii = 1;
|
|
||||||
kbd_resetLEDs(); //update LEDs
|
|
||||||
break;
|
|
||||||
case KBD_SCAN_NUM+KBD_SCAN_RELEASED:
|
|
||||||
kbdFlags ^= KBD_NUM;
|
|
||||||
kbdAscii = 1;
|
|
||||||
kbd_resetLEDs(); //update LEDs
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (kbdAscii == 1)
|
|
||||||
{
|
|
||||||
if (kbdExt > 0)
|
|
||||||
kbdExt--;
|
|
||||||
eoi();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
//====determine ASCII value of key::
|
|
||||||
if (kbdExt > 0) //extended key, kbdScan holds extended key
|
|
||||||
{
|
|
||||||
kbdExt--;
|
|
||||||
kbdAscii = 1;
|
|
||||||
switch (kbdScan)
|
|
||||||
{
|
|
||||||
case KBD_SCANE_ENTER:
|
|
||||||
kbdAscii = '\n'; break;
|
|
||||||
case 53: // '/' character (divide on numpad)
|
|
||||||
kbdAscii = '/'; break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (kbdExt2 > 0) //extended key 2
|
|
||||||
{
|
|
||||||
kbdExt2--;
|
|
||||||
// if (kbdScan == 69) // (pause|break)
|
|
||||||
// kbdAscii = 2; //flag ascii value of 1 means control character (pausebreak)
|
|
||||||
// else
|
|
||||||
kbdAscii = 2; //flag ascii value of 2 means ignore key (or unknown value)
|
|
||||||
}
|
|
||||||
else //not an extended key
|
|
||||||
{
|
|
||||||
// if letter key
|
|
||||||
if (((kbdScan >= 16) && (kbdScan <= 25)) || ((kbdScan >= 30) && (kbdScan <= 38)) || ((kbdScan >= 44) && (kbdScan <= 50)))
|
|
||||||
{
|
|
||||||
// if caps and shift are different (either one pressed, not both)
|
|
||||||
if (((kbdFlags & KBD_SHIFT) != 0) & ((kbdFlags & KBD_CAPS) != 0))
|
|
||||||
kbdAscii = SCAN2ASCII[kbdScan & 0x7F];
|
|
||||||
else if (((kbdFlags & KBD_SHIFT) == 0) & ((kbdFlags & KBD_CAPS) == 0))
|
|
||||||
kbdAscii = SCAN2ASCII[kbdScan & 0x7F];
|
|
||||||
else
|
|
||||||
kbdAscii = SCAN2ASCIISHIFT[kbdScan & 0x7F];
|
|
||||||
}
|
|
||||||
// if numpad key
|
|
||||||
else if ((kbdScan >= 71) && (kbdScan <= 83))
|
|
||||||
{
|
|
||||||
// if numlock on
|
|
||||||
if (kbdFlags & KBD_NUM)
|
|
||||||
kbdAscii = SCAN2ASCIISHIFT[kbdScan & 0x7F];
|
|
||||||
else
|
|
||||||
kbdAscii = SCAN2ASCII[kbdScan & 0x7F];
|
|
||||||
}
|
|
||||||
// other key
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if ((kbdFlags & KBD_SHIFT) != 0)
|
|
||||||
kbdAscii = SCAN2ASCIISHIFT[kbdScan & 0x7F];
|
|
||||||
else
|
|
||||||
kbdAscii = SCAN2ASCII[kbdScan & 0x7F];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//====do something with key::
|
|
||||||
// printf("kbdScan == %d\nkbdAscii == %d\nkbdFlags == %d\n", kbdScan, kbdAscii, kbdFlags);
|
|
||||||
if ((kbdScan == 83) && (kbdFlags & KBD_CTRL) && (kbdFlags & KBD_ALT))
|
|
||||||
{
|
|
||||||
printf("Initiating reboot.");
|
|
||||||
restart();
|
|
||||||
}
|
|
||||||
if (kbdAscii == 2) //unknown key / ignore key
|
|
||||||
{
|
|
||||||
eoi();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (kbdScan < KBD_SCAN_RELEASED) //a key was pressed, save it
|
|
||||||
{
|
|
||||||
if (kbdBufferLen >= KBD_BUFFER_LENGTH) //no key slots available
|
|
||||||
{
|
|
||||||
eoi();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
kbdBuffer[(kbdBufferStart+kbdBufferLen++)%KBD_BUFFER_LENGTH] = (dword) ((kbdFlags << 16) | (kbdScan << 8) | kbdAscii);
|
|
||||||
// printf("S:%d\tL:%d\tR:%x\n", kbdBufferStart, kbdBufferLen, kbdBuffer[kbdBufferStart]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
eoi();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
//Switches the case of an ASCII character
|
|
||||||
/*inline byte (byte asciiCode)
|
|
||||||
{
|
|
||||||
if ((asciiCode >= 'A') & (asciiCode <= 'Z'))
|
|
||||||
return (asciiCode + ('a' - 'A'));
|
|
||||||
if ((asciiCode >= 'a') & (asciiCode <= 'z'))
|
|
||||||
return (asciiCode - ('a' - 'A'));
|
|
||||||
return asciiCode;
|
|
||||||
}*/
|
|
||||||
|
|
||||||
//Gets a key from the buffer, returns 0 if no keys available, returns immediately
|
|
||||||
dword kbdGetKey()
|
|
||||||
{
|
|
||||||
if (kbdBufferLen == 0) //buffer empty
|
|
||||||
return 0;
|
|
||||||
dword retVal = kbdBuffer[kbdBufferStart];
|
|
||||||
kbdBufferStart++;
|
|
||||||
kbdBufferLen--;
|
|
||||||
if (kbdBufferStart >= KBD_BUFFER_LENGTH)
|
|
||||||
kbdBufferStart = 0;
|
|
||||||
return retVal;
|
|
||||||
}
|
|
||||||
|
|
||||||
//Gets a key from the buffer, if no keys available, waits for one to be entered
|
|
||||||
dword kbdWaitKey()
|
|
||||||
{
|
|
||||||
for (;;)
|
|
||||||
{
|
|
||||||
if (kbdBufferLen != 0) //buffer empty
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
dword retVal = kbdBuffer[kbdBufferStart];
|
|
||||||
kbdBufferStart++;
|
|
||||||
kbdBufferLen--;
|
|
||||||
if (kbdBufferStart >= KBD_BUFFER_LENGTH)
|
|
||||||
kbdBufferStart = 0;
|
|
||||||
return retVal;
|
|
||||||
}
|
|
||||||
|
|
||||||
//Resets the keyboard LEDs to reflect the current state of the num lock, caps lock, and scroll lock bits
|
|
||||||
inline void kbd_resetLEDs()
|
|
||||||
{
|
|
||||||
outportb(0x60, 0xED);
|
|
||||||
//outportb(0x60, (kbdFlags & 0x07));
|
|
||||||
ackReason = 0xED;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-47
@@ -1,47 +0,0 @@
|
|||||||
//Keyboard.h
|
|
||||||
// Created: 04/17/03 Josh Holtrop
|
|
||||||
// Modified: 05/07/03
|
|
||||||
//for HOS
|
|
||||||
|
|
||||||
#define KBD_SCROLL 0x01
|
|
||||||
#define KBD_NUM 0x02
|
|
||||||
#define KBD_CAPS 0x04
|
|
||||||
#define KBD_SHIFT 0x10
|
|
||||||
#define KBD_CTRL 0x20
|
|
||||||
#define KBD_ALT 0x40
|
|
||||||
|
|
||||||
#define KBD_SCAN_RELEASED 128
|
|
||||||
|
|
||||||
#define KBD_SCAN_LCTRL 29
|
|
||||||
#define KBD_SCAN_LSHIFT 42
|
|
||||||
#define KBD_SCAN_RSHIFT 54
|
|
||||||
#define KBD_SCAN_LALT 56
|
|
||||||
#define KBD_SCAN_SCROLL 70
|
|
||||||
#define KBD_SCAN_CAPS 58
|
|
||||||
#define KBD_SCAN_NUM 69
|
|
||||||
|
|
||||||
#define KBD_SCANE_PRINTSCREEN 55
|
|
||||||
#define KBD_SCANE_INS 82
|
|
||||||
#define KBD_SCANE_HOME 71
|
|
||||||
#define KBD_SCANE_PGUP 73
|
|
||||||
#define KBD_SCANE_DEL 83
|
|
||||||
#define KBD_SCANE_END 79
|
|
||||||
#define KBD_SCANE_PGDN 81
|
|
||||||
#define KBD_SCANE_ENTER 28
|
|
||||||
|
|
||||||
#define KBD_SCANE_NULL 42
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//====PROTOTYPES:
|
|
||||||
|
|
||||||
void isr_keyboard();
|
|
||||||
inline void kbd_resetLEDs();
|
|
||||||
inline byte kbd_ascii(dword keycode);
|
|
||||||
inline byte kbd_scancode(dword keycode);
|
|
||||||
inline byte kbd_flags(dword keycode);
|
|
||||||
inline byte switchCase(byte asciiCode);
|
|
||||||
dword kbdGetKey();
|
|
||||||
dword kbdWaitKey();
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
del hlibc.a
|
|
||||||
ar -r hlibc.a *.o
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
gcc -ffreestanding -fno-builtin -c *.c
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
nasmw -f aout -l io.lst io.asm -o io_a.o
|
|
||||||
nasmw -f aout -l misc.lst misc.asm -o misc.o
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
|
|
||||||
typedef unsigned int dword;
|
|
||||||
typedef unsigned short word;
|
|
||||||
typedef unsigned char byte;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-532
@@ -1,532 +0,0 @@
|
|||||||
|
|
||||||
%macro jzfar 1
|
|
||||||
jnz %%skip
|
|
||||||
jmp %1
|
|
||||||
%%skip:
|
|
||||||
|
|
||||||
%endmacro
|
|
||||||
|
|
||||||
[global _writeCursorPosition]
|
|
||||||
[global _getCursorPosition]
|
|
||||||
[global _putc]
|
|
||||||
[global _puts]
|
|
||||||
[global _printf]
|
|
||||||
[global _console_scroll]
|
|
||||||
[global _console_cls]
|
|
||||||
[global _putHex]
|
|
||||||
[global _putDec]
|
|
||||||
[global _putDecu]
|
|
||||||
|
|
||||||
|
|
||||||
;
|
|
||||||
;void writeCursorPosition(word pos)
|
|
||||||
;
|
|
||||||
_writeCursorPosition:
|
|
||||||
push ebp
|
|
||||||
mov ebp, esp
|
|
||||||
|
|
||||||
push eax
|
|
||||||
push ebx
|
|
||||||
push edx
|
|
||||||
|
|
||||||
mov eax, [ebp+8] ;cursor position in ax
|
|
||||||
|
|
||||||
mov bl, al
|
|
||||||
mov dx, 0x03D4
|
|
||||||
mov al, 0x0E
|
|
||||||
out dx, al
|
|
||||||
|
|
||||||
inc dx
|
|
||||||
mov al, ah
|
|
||||||
out dx, al
|
|
||||||
|
|
||||||
dec dx
|
|
||||||
mov al, 0x0F
|
|
||||||
out dx, al
|
|
||||||
|
|
||||||
inc dx
|
|
||||||
mov al, bl
|
|
||||||
out dx, al
|
|
||||||
|
|
||||||
pop edx
|
|
||||||
pop ebx
|
|
||||||
pop eax
|
|
||||||
pop ebp
|
|
||||||
|
|
||||||
ret
|
|
||||||
|
|
||||||
|
|
||||||
;
|
|
||||||
;word getCursorPosition()
|
|
||||||
;
|
|
||||||
_getCursorPosition:
|
|
||||||
push ebx
|
|
||||||
push edx
|
|
||||||
|
|
||||||
xor eax, eax
|
|
||||||
mov dx, 0x03D4
|
|
||||||
mov al, 0x0E
|
|
||||||
out dx, al
|
|
||||||
|
|
||||||
inc dx
|
|
||||||
in al, dx
|
|
||||||
mov bl, al
|
|
||||||
|
|
||||||
dec dx
|
|
||||||
mov al, 0x0F
|
|
||||||
out dx, al
|
|
||||||
|
|
||||||
inc dx
|
|
||||||
in al, dx
|
|
||||||
mov ah, bl
|
|
||||||
|
|
||||||
pop edx
|
|
||||||
pop ebx
|
|
||||||
|
|
||||||
ret
|
|
||||||
|
|
||||||
|
|
||||||
;
|
|
||||||
;int putc(int chr)
|
|
||||||
;
|
|
||||||
_putc:
|
|
||||||
push ebp
|
|
||||||
mov ebp, esp
|
|
||||||
push ebx
|
|
||||||
push ecx
|
|
||||||
push edx
|
|
||||||
|
|
||||||
call _getCursorPosition
|
|
||||||
mov ebx, eax
|
|
||||||
mov ecx, ebx
|
|
||||||
|
|
||||||
mov eax, [ebp+8] ;al=character
|
|
||||||
cmp al, 10 ;newline
|
|
||||||
jz putc_newline
|
|
||||||
cmp al, 9 ;tab
|
|
||||||
jz putc_tab
|
|
||||||
|
|
||||||
shl ebx, 1
|
|
||||||
add ebx, 0xb8000
|
|
||||||
mov ah, 0x07
|
|
||||||
mov [ebx], ax
|
|
||||||
mov eax, ecx
|
|
||||||
inc eax
|
|
||||||
cmp eax, 2000
|
|
||||||
jnz putc_writeit2
|
|
||||||
call _console_scroll
|
|
||||||
mov eax, 2000-80
|
|
||||||
putc_writeit2:
|
|
||||||
push eax
|
|
||||||
call _writeCursorPosition
|
|
||||||
add esp, 4
|
|
||||||
jmp putc_done
|
|
||||||
|
|
||||||
putc_newline:
|
|
||||||
mov eax, ebx ;eax = cursor position
|
|
||||||
mov ebx, 80
|
|
||||||
xor edx, edx
|
|
||||||
div bx ;ax=dx:ax/bx, dx=remainder
|
|
||||||
mov bx, 80
|
|
||||||
sub bx, dx
|
|
||||||
mov eax, ecx
|
|
||||||
add eax, ebx ;eax = new cursor position
|
|
||||||
cmp eax, 2000
|
|
||||||
jnz putc_newline_writeit2
|
|
||||||
call _console_scroll
|
|
||||||
mov eax, 2000-80 ;beginning of last row
|
|
||||||
putc_newline_writeit2:
|
|
||||||
push eax
|
|
||||||
call _writeCursorPosition
|
|
||||||
add esp, 4
|
|
||||||
jmp putc_done
|
|
||||||
|
|
||||||
putc_tab:
|
|
||||||
mov eax, ebx ;eax = cursor position
|
|
||||||
mov ebx, 8
|
|
||||||
div bl ;al=ax/bl, ah=remainder
|
|
||||||
xor edx, edx
|
|
||||||
mov dl, ah
|
|
||||||
mov bx, 8
|
|
||||||
sub bx, dx
|
|
||||||
mov eax, ecx
|
|
||||||
add eax, ebx ;eax = new cursor position
|
|
||||||
cmp eax, 2000
|
|
||||||
jnz putc_tab_writeit2
|
|
||||||
call _console_scroll
|
|
||||||
mov eax, 2000-80 ;beginning of last row
|
|
||||||
putc_tab_writeit2:
|
|
||||||
push eax
|
|
||||||
call _writeCursorPosition
|
|
||||||
add esp, 4
|
|
||||||
|
|
||||||
putc_done:
|
|
||||||
pop edx
|
|
||||||
pop ecx
|
|
||||||
pop ebx
|
|
||||||
pop ebp
|
|
||||||
|
|
||||||
ret
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
;
|
|
||||||
;void printf(char *fmt, ... )
|
|
||||||
;
|
|
||||||
_printf:
|
|
||||||
push ebp
|
|
||||||
mov ebp, esp
|
|
||||||
pusha
|
|
||||||
mov ebx, [ebp+8] ;ebx = position in format string
|
|
||||||
mov esi, ebp
|
|
||||||
add esi, 12 ;esi = to next variable arg
|
|
||||||
xor ecx, ecx ;ecx used if we encounter a '%'
|
|
||||||
printf_loop:
|
|
||||||
mov al, [ebx]
|
|
||||||
inc ebx
|
|
||||||
cmp al, 0
|
|
||||||
jzfar printf_done
|
|
||||||
cmp ecx, 1
|
|
||||||
jz printf_special
|
|
||||||
cmp al, '%'
|
|
||||||
jzfar printf_percent
|
|
||||||
|
|
||||||
push eax
|
|
||||||
call _putc
|
|
||||||
add esp, 4
|
|
||||||
jmp printf_loop
|
|
||||||
|
|
||||||
printf_special:
|
|
||||||
xor ecx, ecx
|
|
||||||
cmp al, 'd'
|
|
||||||
jz printf_decimal
|
|
||||||
cmp al, 'u'
|
|
||||||
jz printf_decimalu
|
|
||||||
cmp al, 'x'
|
|
||||||
jz printf_hex
|
|
||||||
cmp al, '%'
|
|
||||||
jz printf_ppercent
|
|
||||||
cmp al, 's'
|
|
||||||
jz printf_string
|
|
||||||
cmp al, 'c'
|
|
||||||
jz printf_char
|
|
||||||
jmp printf_special_done
|
|
||||||
|
|
||||||
printf_decimal:
|
|
||||||
mov eax, [esi]
|
|
||||||
push eax
|
|
||||||
call _putDec
|
|
||||||
add esp, 4
|
|
||||||
jmp printf_special_done
|
|
||||||
|
|
||||||
printf_decimalu:
|
|
||||||
mov eax, [esi]
|
|
||||||
push eax
|
|
||||||
call _putDecu
|
|
||||||
add esp, 4
|
|
||||||
jmp printf_special_done
|
|
||||||
|
|
||||||
printf_hex:
|
|
||||||
mov eax, [esi]
|
|
||||||
push eax
|
|
||||||
call _putHex
|
|
||||||
add esp, 4
|
|
||||||
jmp printf_special_done
|
|
||||||
|
|
||||||
printf_ppercent:
|
|
||||||
push eax
|
|
||||||
call _putc
|
|
||||||
add esp, 4
|
|
||||||
jmp printf_special_done
|
|
||||||
|
|
||||||
printf_string:
|
|
||||||
mov eax, [esi]
|
|
||||||
push eax
|
|
||||||
call _puts
|
|
||||||
add esp, 4
|
|
||||||
jmp printf_special_done
|
|
||||||
|
|
||||||
printf_char:
|
|
||||||
mov eax, [esi]
|
|
||||||
push eax
|
|
||||||
call _putc
|
|
||||||
add esp, 4
|
|
||||||
jmp printf_special_done
|
|
||||||
|
|
||||||
printf_special_done
|
|
||||||
add esi, 4 ;point to next extra argument
|
|
||||||
jmp printf_loop
|
|
||||||
|
|
||||||
printf_percent:
|
|
||||||
mov ecx, 1
|
|
||||||
jmp printf_loop
|
|
||||||
|
|
||||||
|
|
||||||
printf_done:
|
|
||||||
popa
|
|
||||||
pop ebp
|
|
||||||
ret
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
;
|
|
||||||
;void console_scroll()
|
|
||||||
;
|
|
||||||
_console_scroll:
|
|
||||||
pusha
|
|
||||||
mov esi, 0xb8000+160
|
|
||||||
mov edi, 0xb8000
|
|
||||||
mov ecx, 960 ;(2000-80)/2
|
|
||||||
console_scroll_loop:
|
|
||||||
lodsd
|
|
||||||
stosd
|
|
||||||
loop console_scroll_loop
|
|
||||||
mov ax, 0x0720
|
|
||||||
mov ecx, 80
|
|
||||||
console_scroll_loop2:
|
|
||||||
stosw
|
|
||||||
loop console_scroll_loop2
|
|
||||||
popa
|
|
||||||
ret
|
|
||||||
|
|
||||||
;
|
|
||||||
;void console_cls()
|
|
||||||
;
|
|
||||||
_console_cls:
|
|
||||||
pusha
|
|
||||||
mov edi, 0xb8000
|
|
||||||
mov ax, 0x0720
|
|
||||||
mov ecx, 2000
|
|
||||||
console_cls_loop:
|
|
||||||
stosw
|
|
||||||
loop console_cls_loop
|
|
||||||
push dword 0
|
|
||||||
call _writeCursorPosition
|
|
||||||
add esp, 4
|
|
||||||
popa
|
|
||||||
ret
|
|
||||||
|
|
||||||
;
|
|
||||||
;int putHex(dword number)
|
|
||||||
;
|
|
||||||
_putHex:
|
|
||||||
push ebp
|
|
||||||
mov ebp, esp
|
|
||||||
pusha
|
|
||||||
mov eax, [ebp+8] ;eax = number to print
|
|
||||||
xor ebx, ebx ;we have not printed a character yet
|
|
||||||
mov ecx, 8 ;counter for number of characters
|
|
||||||
|
|
||||||
putHex_loop:
|
|
||||||
push eax
|
|
||||||
push ecx
|
|
||||||
|
|
||||||
dec ecx
|
|
||||||
shl ecx, 2 ;edx=counter*4 (amount to shift by)
|
|
||||||
shr eax, cl
|
|
||||||
and eax, 0x0F
|
|
||||||
cmp cl, 0
|
|
||||||
jz putHex_notzero ;if number is 0
|
|
||||||
cmp al, 0
|
|
||||||
jnz putHex_notzero
|
|
||||||
cmp bl, 0
|
|
||||||
jz putHex_loop_end
|
|
||||||
putHex_notzero:
|
|
||||||
mov bl, 1
|
|
||||||
add eax, '0'
|
|
||||||
cmp eax, '9'
|
|
||||||
jbe putHex_dontadjust
|
|
||||||
add eax, 'A'-'9'-1
|
|
||||||
putHex_dontadjust:
|
|
||||||
push eax
|
|
||||||
call _putc
|
|
||||||
add esp, 4
|
|
||||||
|
|
||||||
putHex_loop_end:
|
|
||||||
pop ecx
|
|
||||||
pop eax
|
|
||||||
loop putHex_loop
|
|
||||||
|
|
||||||
popa
|
|
||||||
pop ebp
|
|
||||||
ret
|
|
||||||
|
|
||||||
|
|
||||||
;
|
|
||||||
;int puts(char *str)
|
|
||||||
;
|
|
||||||
_puts:
|
|
||||||
push ebp
|
|
||||||
mov ebp, esp
|
|
||||||
push esi
|
|
||||||
push eax
|
|
||||||
mov esi, [ebp+8] ;esi = to string
|
|
||||||
puts_loop:
|
|
||||||
lodsb
|
|
||||||
cmp al, 0
|
|
||||||
jz puts_done
|
|
||||||
push eax
|
|
||||||
call _putc
|
|
||||||
add esp, 4
|
|
||||||
jmp puts_loop
|
|
||||||
|
|
||||||
puts_done:
|
|
||||||
pop eax
|
|
||||||
pop esi
|
|
||||||
pop ebp
|
|
||||||
ret
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
_putDecu:
|
|
||||||
push ebp
|
|
||||||
mov ebp, esp
|
|
||||||
sub esp, 24
|
|
||||||
mov DWORD [ebp-4], 1
|
|
||||||
mov BYTE [ebp-5], 0
|
|
||||||
L2:
|
|
||||||
mov edx, DWORD [ebp+8]
|
|
||||||
mov eax, -858993459
|
|
||||||
mul edx
|
|
||||||
mov eax, edx
|
|
||||||
shr eax, 3
|
|
||||||
cmp eax, DWORD [ebp-4]
|
|
||||||
jae L4
|
|
||||||
jmp L3
|
|
||||||
L4:
|
|
||||||
mov eax, DWORD [ebp-4]
|
|
||||||
mov edx, eax
|
|
||||||
sal edx, 2
|
|
||||||
add edx, eax
|
|
||||||
lea eax, [edx+edx]
|
|
||||||
mov DWORD [ebp-4], eax
|
|
||||||
jmp L2
|
|
||||||
L3:
|
|
||||||
nop
|
|
||||||
L5:
|
|
||||||
cmp DWORD [ebp-4], 1
|
|
||||||
ja L7
|
|
||||||
jmp L6
|
|
||||||
L7:
|
|
||||||
mov edx, DWORD [ebp+8]
|
|
||||||
mov eax, edx
|
|
||||||
mov edx, 0
|
|
||||||
div DWORD [ebp-4]
|
|
||||||
mov DWORD [ebp-12], eax
|
|
||||||
mov al, BYTE [ebp-12]
|
|
||||||
mov BYTE [ebp-5], al
|
|
||||||
mov eax, 0
|
|
||||||
mov al, BYTE [ebp-5]
|
|
||||||
imul eax, DWORD [ebp-4]
|
|
||||||
sub DWORD [ebp+8], eax
|
|
||||||
mov edx, DWORD [ebp-4]
|
|
||||||
mov eax, -858993459
|
|
||||||
mul edx
|
|
||||||
mov eax, edx
|
|
||||||
shr eax, 3
|
|
||||||
mov DWORD [ebp-4], eax
|
|
||||||
lea eax, [ebp-5]
|
|
||||||
add BYTE [eax], 48
|
|
||||||
sub esp, 12
|
|
||||||
mov eax, 0
|
|
||||||
mov al, BYTE [ebp-5]
|
|
||||||
push eax
|
|
||||||
call _putc
|
|
||||||
add esp, 16
|
|
||||||
jmp L5
|
|
||||||
L6:
|
|
||||||
sub esp, 12
|
|
||||||
mov al, BYTE [ebp+8]
|
|
||||||
add eax, 48
|
|
||||||
and eax, 255
|
|
||||||
push eax
|
|
||||||
call _putc
|
|
||||||
add esp, 16
|
|
||||||
leave
|
|
||||||
ret
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
_putDec:
|
|
||||||
push ebp
|
|
||||||
mov ebp, esp
|
|
||||||
sub esp, 24
|
|
||||||
cmp DWORD [ebp+8], 0
|
|
||||||
jns L9
|
|
||||||
sub esp, 12
|
|
||||||
push 45
|
|
||||||
call _putc
|
|
||||||
add esp, 16
|
|
||||||
neg DWORD [ebp+8]
|
|
||||||
L9:
|
|
||||||
mov DWORD [ebp-4], 1
|
|
||||||
mov BYTE [ebp-5], 0
|
|
||||||
L10:
|
|
||||||
mov eax, DWORD [ebp+8]
|
|
||||||
cmp eax, DWORD [ebp-4]
|
|
||||||
jae L12
|
|
||||||
jmp L11
|
|
||||||
L12:
|
|
||||||
mov eax, DWORD [ebp-4]
|
|
||||||
mov edx, eax
|
|
||||||
sal edx, 2
|
|
||||||
add edx, eax
|
|
||||||
lea eax, [edx+edx]
|
|
||||||
mov DWORD [ebp-4], eax
|
|
||||||
jmp L10
|
|
||||||
L11:
|
|
||||||
mov edx, DWORD [ebp-4]
|
|
||||||
mov eax, -858993459
|
|
||||||
mul edx
|
|
||||||
mov eax, edx
|
|
||||||
shr eax, 3
|
|
||||||
mov DWORD [ebp-4], eax
|
|
||||||
L13:
|
|
||||||
cmp DWORD [ebp-4], 1
|
|
||||||
ja L15
|
|
||||||
jmp L14
|
|
||||||
L15:
|
|
||||||
mov edx, DWORD [ebp+8]
|
|
||||||
mov eax, edx
|
|
||||||
mov edx, 0
|
|
||||||
div DWORD [ebp-4]
|
|
||||||
mov DWORD [ebp-12], eax
|
|
||||||
mov al, BYTE [ebp-12]
|
|
||||||
mov BYTE [ebp-5], al
|
|
||||||
mov eax, 0
|
|
||||||
mov al, BYTE [ebp-5]
|
|
||||||
imul eax, DWORD [ebp-4]
|
|
||||||
sub DWORD [ebp+8], eax
|
|
||||||
mov edx, DWORD [ebp-4]
|
|
||||||
mov eax, -858993459
|
|
||||||
mul edx
|
|
||||||
mov eax, edx
|
|
||||||
shr eax, 3
|
|
||||||
mov DWORD [ebp-4], eax
|
|
||||||
lea eax, [ebp-5]
|
|
||||||
add BYTE [eax], 48
|
|
||||||
sub esp, 12
|
|
||||||
mov eax, 0
|
|
||||||
mov al, BYTE [ebp-5]
|
|
||||||
push eax
|
|
||||||
call _putc
|
|
||||||
add esp, 16
|
|
||||||
jmp L13
|
|
||||||
L14:
|
|
||||||
sub esp, 12
|
|
||||||
mov al, BYTE [ebp+8]
|
|
||||||
add eax, 48
|
|
||||||
and eax, 255
|
|
||||||
push eax
|
|
||||||
call _putc
|
|
||||||
add esp, 16
|
|
||||||
leave
|
|
||||||
ret
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
|
|
||||||
|
|
||||||
#ifndef __HIO_H__
|
|
||||||
#define __HIO_H__ __HIO_H__
|
|
||||||
|
|
||||||
void writeCursorPosition(dword pos);
|
|
||||||
dword getCursorPosition();
|
|
||||||
int putc(dword chr);
|
|
||||||
int printf(char *fmt, ...);
|
|
||||||
int puts(char *str);
|
|
||||||
int putDec(int number);
|
|
||||||
int putDecu(dword number);
|
|
||||||
int putHex(dword number);
|
|
||||||
void console_cls();
|
|
||||||
void console_scroll();
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
;void memcpy(dword src, dword dest, dword length)
|
|
||||||
[global _memcpy]
|
|
||||||
_memcpy:
|
|
||||||
push ebp
|
|
||||||
mov ebp, esp
|
|
||||||
push esi
|
|
||||||
push edi
|
|
||||||
push ecx
|
|
||||||
mov esi, [ebp+8]
|
|
||||||
mov edi, [ebp+12]
|
|
||||||
mov ecx, [ebp+14]
|
|
||||||
|
|
||||||
rep movsb
|
|
||||||
|
|
||||||
pop ecx
|
|
||||||
pop edi
|
|
||||||
pop esi
|
|
||||||
pop ebp
|
|
||||||
ret
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
|
|
||||||
#ifndef __HMISC_H__
|
|
||||||
#define __HMISC_H__ __HMISC_H__
|
|
||||||
|
|
||||||
void memcpy(dword src, dword dest, dword length);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
|
|
||||||
#include "string.h"
|
|
||||||
|
|
||||||
int strlen(char *str)
|
|
||||||
{
|
|
||||||
int retn = 0;
|
|
||||||
while (*str++ != 0)
|
|
||||||
retn++;
|
|
||||||
return retn;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
|
|
||||||
#ifndef __HSTRING_H__
|
|
||||||
#define __HSTRING_H__ __HSTRING_H__
|
|
||||||
|
|
||||||
int strlen(char *str);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
ld -nodefaultlibs -nostdlib -T link.ld -o kernel.bin -Map .\lst\LDout.doc ks.o kernel.o asmfuncs.o .\lib\hlibc.a
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
OUTPUT_FORMAT("binary")
|
|
||||||
ENTRY(start)
|
|
||||||
SECTIONS
|
|
||||||
{
|
|
||||||
.text 0x104000 : {
|
|
||||||
code = .; _code = .; __code = .;
|
|
||||||
*(.text)
|
|
||||||
. = ALIGN(4096);
|
|
||||||
}
|
|
||||||
.data : {
|
|
||||||
data = .; _data = .; __data = .;
|
|
||||||
*(.data)
|
|
||||||
. = ALIGN(4096);
|
|
||||||
}
|
|
||||||
.bss :
|
|
||||||
{
|
|
||||||
bss = .; _bss = .; __bss = .;
|
|
||||||
*(.bss)
|
|
||||||
. = ALIGN(4096);
|
|
||||||
}
|
|
||||||
end = .; _end = .; __end = .;
|
|
||||||
}
|
|
||||||
@@ -1,282 +0,0 @@
|
|||||||
// mm.c
|
|
||||||
// 09/01/03 Josh Holtrop
|
|
||||||
|
|
||||||
// 0x368000 is first available byte (this is right after the kernel @ 1mb and the initrd @ 2mb, 1440kb.
|
|
||||||
|
|
||||||
|
|
||||||
//Pointer to the first pageblock in the linked list
|
|
||||||
pageblock *first_pageblock = (pageblock *) 0;
|
|
||||||
//The total amount of physical memory available (bytes)
|
|
||||||
dword mm_totalmem = 0x100000; //start counting from 1mb
|
|
||||||
//The highest physical address
|
|
||||||
dword mm_highestAddress = 0;
|
|
||||||
|
|
||||||
//This function initializes the memory manager's linked list, filling it with the
|
|
||||||
// memory areas returned by bios interrupt 0x8E20
|
|
||||||
void mm_init()
|
|
||||||
{
|
|
||||||
dword *memmap_entries = (dword *) 0x9040A;
|
|
||||||
memmap_entry *maps = (memmap_entry *) 0x92000;
|
|
||||||
dword a;
|
|
||||||
for (a=0;a<(*memmap_entries);a++)
|
|
||||||
{
|
|
||||||
if (maps[a].attributes == 1) // (1) mem free to OS
|
|
||||||
{
|
|
||||||
mm_totalmem += maps[a].limit.lowdword;
|
|
||||||
if (mm_highestAddress < (maps[a].base.lowdword + maps[a].limit.lowdword))
|
|
||||||
mm_highestAddress = maps[a].base.lowdword + maps[a].limit.lowdword;
|
|
||||||
if ((maps[a].base.lowdword + maps[a].limit.lowdword) > (FREERAM_START+8192)) //goes past where we start freeram
|
|
||||||
{
|
|
||||||
if (maps[a].base.lowdword < FREERAM_START)
|
|
||||||
{
|
|
||||||
maps[a].limit.lowdword = maps[a].limit.lowdword - (FREERAM_START - maps[a].base.lowdword);
|
|
||||||
maps[a].base.lowdword = FREERAM_START; //block at least 4kb, starts >= FREERAM_START
|
|
||||||
}
|
|
||||||
if (first_pageblock == 0) //no pageblock page set up yet, so set it up here
|
|
||||||
{
|
|
||||||
first_pageblock = (pageblock *) maps[a].base.lowdword;
|
|
||||||
maps[a].base.lowdword += 4096;
|
|
||||||
maps[a].limit.lowdword -= 4096;
|
|
||||||
mm_init_pageblockpage(first_pageblock);
|
|
||||||
first_pageblock->base = maps[a].base.lowdword;
|
|
||||||
first_pageblock->length = maps[a].limit.lowdword / 4096;
|
|
||||||
first_pageblock->pid = MM_PB_AVAIL;
|
|
||||||
}
|
|
||||||
else //first_pageblock already set up, add on segment
|
|
||||||
{
|
|
||||||
pageblock *pb = first_pageblock;
|
|
||||||
for (;;)
|
|
||||||
{
|
|
||||||
if (pb->pid == MM_PB_NP)
|
|
||||||
break;
|
|
||||||
else
|
|
||||||
pb++;
|
|
||||||
}
|
|
||||||
pb->base = maps[a].base.lowdword;
|
|
||||||
pb->length = maps[a].limit.lowdword / 4096;
|
|
||||||
pb->pid = MM_PB_AVAIL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (first_pageblock == 0)
|
|
||||||
{
|
|
||||||
printf("ERROR! NO INITIAL PAGE BLOCK CREATED.");
|
|
||||||
asm("cli");
|
|
||||||
asm("hlt");
|
|
||||||
for (;;)
|
|
||||||
;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//This function initializes a pageblock to be full of empty pointers
|
|
||||||
void mm_init_pageblockpage(pageblock *pbp)
|
|
||||||
{
|
|
||||||
int a;
|
|
||||||
for (a=0; a<256; a++) // 256 pageblock entries * 16 bytes per page block entry = 4096 bytes (1 page of pageblock entries)
|
|
||||||
{
|
|
||||||
pbp[a].base = 0;
|
|
||||||
pbp[a].length = 0;
|
|
||||||
pbp[a].pid = MM_PB_NP;
|
|
||||||
if (a<255)
|
|
||||||
pbp[a].link = (dword)(&pbp[a+1]);
|
|
||||||
else
|
|
||||||
pbp[a].link = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//This function allocates a certain number of physical pages for a particular process
|
|
||||||
//Returns:
|
|
||||||
// 0 - no free mem, invalied PID
|
|
||||||
// void* - physical address of allocated pages
|
|
||||||
void *mm_palloc(dword numpages, dword proc_pid)
|
|
||||||
{
|
|
||||||
if (proc_pid < PID_KERNEL)
|
|
||||||
return 0;
|
|
||||||
pageblock *pb = first_pageblock;
|
|
||||||
if (mm_freeentries() < 2)
|
|
||||||
{
|
|
||||||
if(mm_new_pageblock_page() == 0)
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
pageblock *newentry = mm_nextpageblockentry();
|
|
||||||
for (;;)
|
|
||||||
{
|
|
||||||
if ((pb->pid == MM_PB_AVAIL) && (pb->length >= numpages))
|
|
||||||
break;
|
|
||||||
if (pb->link == 0)
|
|
||||||
return 0;
|
|
||||||
pb = (pageblock *)pb->link;
|
|
||||||
}
|
|
||||||
if (pb->length == numpages) //dont need a new entry, just mark this one as used :)
|
|
||||||
{
|
|
||||||
pb->pid = proc_pid;
|
|
||||||
return (void *)pb->base;
|
|
||||||
}
|
|
||||||
else //subtract out allocated number of pages from length
|
|
||||||
{
|
|
||||||
newentry->base = pb->base;
|
|
||||||
newentry->length = numpages;
|
|
||||||
newentry->pid = proc_pid;
|
|
||||||
pb->base += (numpages * 4096);
|
|
||||||
pb->length -= numpages;
|
|
||||||
return (void *)newentry->base;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//This method returns the number of free pageblock entries at the end of the linked list
|
|
||||||
dword mm_freeentries()
|
|
||||||
{
|
|
||||||
pageblock *pb = first_pageblock;
|
|
||||||
dword counter = 0;
|
|
||||||
for (;;)
|
|
||||||
{
|
|
||||||
if (pb->pid == MM_PB_NP)
|
|
||||||
counter++;
|
|
||||||
if (pb->link == 0)
|
|
||||||
return counter;
|
|
||||||
pb = (pageblock *) pb->link;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//This method creates a new page used to hold more pageblock entries at the end of the linked list
|
|
||||||
//Returns:
|
|
||||||
// 0 - no free pages
|
|
||||||
// pageblock* - address of pageblock page
|
|
||||||
pageblock *mm_new_pageblock_page() //as of 09/26/03 this method leaks 4kb main memory per call, unrecoverable
|
|
||||||
{
|
|
||||||
pageblock *pb = first_pageblock;
|
|
||||||
for (;;)
|
|
||||||
{
|
|
||||||
if ((pb->pid == MM_PB_AVAIL) && (pb->length > 1))
|
|
||||||
{
|
|
||||||
pageblock *retval = (pageblock *)pb->base;
|
|
||||||
pb->base += 4096;
|
|
||||||
pb->length--;
|
|
||||||
mm_init_pageblockpage(retval); //zeros out, links new pageblock page
|
|
||||||
mm_lastpageblockentry()->link = (dword)retval; //set up link to new pageblock page
|
|
||||||
return retval;
|
|
||||||
}
|
|
||||||
pb = (pageblock *) pb->link;
|
|
||||||
if (pb == 0)
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//This method frees a physical page
|
|
||||||
//Returns:
|
|
||||||
// 2 - pointer to free was a null pointer
|
|
||||||
// 1 - address not found
|
|
||||||
// 0 - all went well
|
|
||||||
int mm_pfree(void *ptr)
|
|
||||||
{
|
|
||||||
if (ptr == 0)
|
|
||||||
return 2;
|
|
||||||
pageblock *pb = first_pageblock;
|
|
||||||
dword tofree = (dword) ptr;
|
|
||||||
for (;;)
|
|
||||||
{
|
|
||||||
if (pb->base == tofree) // && (pb->pid == MM_PB_USED))
|
|
||||||
{
|
|
||||||
pb->pid = MM_PB_AVAIL; //found block, mark available / coalesce it
|
|
||||||
mm_coalesce(pb);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
if (pb->link == 0)
|
|
||||||
return 1;
|
|
||||||
pb = (pageblock *) pb->link;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//This method returns a pointer to the last pageblock in the linked list
|
|
||||||
pageblock *mm_lastpageblockentry()
|
|
||||||
{
|
|
||||||
pageblock *pb = first_pageblock;
|
|
||||||
for (;;)
|
|
||||||
{
|
|
||||||
if (pb->link == 0)
|
|
||||||
return pb;
|
|
||||||
pb = (pageblock *)pb->link;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//This method returns the next available-for-use pageblock entry
|
|
||||||
// or 0, if they are all used
|
|
||||||
pageblock *mm_nextpageblockentry()
|
|
||||||
{
|
|
||||||
pageblock *pb = first_pageblock;
|
|
||||||
for (;;)
|
|
||||||
{
|
|
||||||
if (pb->pid == MM_PB_NP)
|
|
||||||
return pb;
|
|
||||||
if (pb->link == 0)
|
|
||||||
return 0;
|
|
||||||
pb = (pageblock *)pb->link;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//This method returns the amount of free physical RAM
|
|
||||||
dword mm_freemem()
|
|
||||||
{
|
|
||||||
dword amount = 0;
|
|
||||||
pageblock *pb = first_pageblock;
|
|
||||||
for (;;)
|
|
||||||
{
|
|
||||||
if (pb->pid == MM_PB_AVAIL)
|
|
||||||
amount += (pb->length)*4096;
|
|
||||||
if (pb->link == 0)
|
|
||||||
return amount;
|
|
||||||
pb = (pageblock *) pb->link;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//This method "walks" through the linked list of pageblock entries and
|
|
||||||
// combines any two entries that are adjacent and of the same type
|
|
||||||
void mm_coalesce(pageblock *pb)
|
|
||||||
{
|
|
||||||
pageblock *pbc = first_pageblock;
|
|
||||||
for (;;)
|
|
||||||
{
|
|
||||||
if (pbc->pid == pb->pid) //pid fields must match, both same process, both avail, or both not present
|
|
||||||
{
|
|
||||||
if ((pbc->base + (pbc->length * 4096)) == pb->base) //pbc ends where pb starts
|
|
||||||
{
|
|
||||||
pbc->length += pb->length;
|
|
||||||
pb->pid = MM_PB_NP;
|
|
||||||
mm_coalesce(pbc); //recursion: checks for any more page blocks to coalesce if one found
|
|
||||||
return; //exit this recursion...
|
|
||||||
}
|
|
||||||
if ((pb->base + (pb->length * 4096)) == pbc->base) //pb ends where pbc starts
|
|
||||||
{
|
|
||||||
pb->length += pbc->length;
|
|
||||||
pbc->pid = MM_PB_NP;
|
|
||||||
mm_coalesce(pb);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (pbc->link == 0)
|
|
||||||
return;
|
|
||||||
pbc = (pageblock *)pbc->link;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
qword base;
|
|
||||||
qword limit;
|
|
||||||
dword attributes;
|
|
||||||
} __attribute__((packed)) memmap_entry;
|
|
||||||
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
dword base;
|
|
||||||
dword length; //in pages
|
|
||||||
dword pid; //normally PID of process, use NP, AVAIL for special flags
|
|
||||||
dword link; //leave dword instead of pointer so i dont have to worry about pointer arithmetic
|
|
||||||
} __attribute__ ((packed)) pageblock; //16 byte pageblock entry - 256 entries = 1 page
|
|
||||||
|
|
||||||
|
|
||||||
void mm_init();
|
|
||||||
void mm_init_pageblockpage(pageblock *pbp);
|
|
||||||
void *mm_palloc(dword numpages, dword proc_pid);
|
|
||||||
void mm_coalesce(pageblock *pb);
|
|
||||||
int mm_pfree(void *ptr);
|
|
||||||
dword mm_freeentries();
|
|
||||||
dword mm_freemem();
|
|
||||||
pageblock *mm_new_pageblock_page();
|
|
||||||
pageblock *mm_lastpageblockentry();
|
|
||||||
pageblock *mm_nextpageblockentry();
|
|
||||||
|
|
||||||
|
|
||||||
#define MM_PB_NP 0x00 //00000000
|
|
||||||
#define MM_PB_AVAIL 0x01 //00000001
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,68 +0,0 @@
|
|||||||
// mouse.c
|
|
||||||
// 10/03/03
|
|
||||||
// Author: Josh Holtrop
|
|
||||||
|
|
||||||
|
|
||||||
//This method initializes the ps/2 mouse
|
|
||||||
void mouse_init()
|
|
||||||
{
|
|
||||||
outportb(0x64, 0x20); //tell keyboard controller we are going to read keyboard controller command byte
|
|
||||||
byte temp = inportb(0x60); //read keyboard controller command byte
|
|
||||||
outportb(0x64, 0x60); //tell keyboard controller we are going to write keyboard controller command byte
|
|
||||||
outportb(0x60, 0x03 | (temp&0x40)); //write keyboard controller command byte: enable mouse/keyboard ints, include original XLATE bit from temp (bit6)
|
|
||||||
|
|
||||||
outportb(0x64, 0xA8); //enable mouse port
|
|
||||||
|
|
||||||
outportb(0x64, 0xD4); //send command to mouse, not kbd
|
|
||||||
outportb(0x60, 0xF4); //enable data reporting
|
|
||||||
|
|
||||||
mouse_x = video_mode.XResolution >> 1;
|
|
||||||
mouse_y = video_mode.YResolution >> 1;
|
|
||||||
|
|
||||||
//outportb(0x64, 0xD4);
|
|
||||||
//outportb(0x60, 0xE7); //scaling 2:1
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//This method is called when a mouse interrupt occurs
|
|
||||||
void isr_mouse()
|
|
||||||
{
|
|
||||||
byte inb = inportb(0x60); //read mouse byte
|
|
||||||
//printf("InB: %x\n", inb);
|
|
||||||
//return;
|
|
||||||
if ((inb == 0xFA) && (mouse_bytesRead < 1)) //ACK
|
|
||||||
return;
|
|
||||||
mouse_inbuffer[mouse_bytesRead] = inb;
|
|
||||||
mouse_bytesRead++;
|
|
||||||
if (mouse_bytesRead == 3) //complete packet received
|
|
||||||
{
|
|
||||||
mouse_bytesRead = 0;
|
|
||||||
int adjx = (char) mouse_inbuffer[1];
|
|
||||||
int adjy = (char) mouse_inbuffer[2];
|
|
||||||
mouse_x += adjx;
|
|
||||||
mouse_y -= adjy; //-= because screen y coordinates are opposite mouse y coordinates
|
|
||||||
if (mouse_x < 0)
|
|
||||||
mouse_x = 0;
|
|
||||||
if (mouse_x >= video_mode.XResolution)
|
|
||||||
mouse_x = video_mode.XResolution - 1;
|
|
||||||
if (mouse_y < 0)
|
|
||||||
mouse_y = 0;
|
|
||||||
if (mouse_y >= video_mode.YResolution)
|
|
||||||
mouse_y = video_mode.YResolution - 1;
|
|
||||||
if (mouse_inbuffer[0] & 0x01) //left button
|
|
||||||
{
|
|
||||||
video_pset(mouse_x, mouse_y, 0x00FF8800);
|
|
||||||
printf("X: %d, Y: %d\n", mouse_x, mouse_y);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
video_pset(mouse_x, mouse_y, 0x00FFFFFF);
|
|
||||||
printf("X: %d, Y: %d\n", mouse_x, mouse_y);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
// mouse.h
|
|
||||||
// 10/03/03
|
|
||||||
// Author: Josh Holtrop
|
|
||||||
|
|
||||||
|
|
||||||
int mouse_x;
|
|
||||||
int mouse_y;
|
|
||||||
int mouse_bytesRead = 0;
|
|
||||||
byte mouse_inbuffer[16];
|
|
||||||
|
|
||||||
void mouse_init();
|
|
||||||
void isr_mouse();
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
+89
@@ -0,0 +1,89 @@
|
|||||||
|
HOS - Holtrop's Operating System
|
||||||
|
--------------------------------
|
||||||
|
|
||||||
|
HOS is (in the process of becoming) a 32-bit, protected mode, graphical, multitasking operating system.
|
||||||
|
It was written by me, Josh Holtrop, with help from a few others along the way.
|
||||||
|
|
||||||
|
|
||||||
|
Goals: (A = accomplished, P = in progress, T = todo)
|
||||||
|
----------------------------------------------------
|
||||||
|
(A) Custom bootloader to load kernel from FAT-formatted boot media, options for video mode/ram disk
|
||||||
|
(A) Multiboot compliance - kernel can be loaded by GRUB
|
||||||
|
(A) 32-bit protected mode environment
|
||||||
|
(A) VESA Support for graphics modes
|
||||||
|
(A) PS/2 keyboard & mouse drivers
|
||||||
|
(A) Utilize x86's paging architecture for virtual memory management
|
||||||
|
|
||||||
|
(P) Console Manager
|
||||||
|
(P) VFS abstraction layer for a single file system
|
||||||
|
(P) ram disk driver
|
||||||
|
(P) devfs file system driver
|
||||||
|
(P) ext2 file system support
|
||||||
|
|
||||||
|
(T) vfat file system support
|
||||||
|
(T) Multitasking support
|
||||||
|
(T) HASH command shell
|
||||||
|
(T) Window Manager
|
||||||
|
(T) Various other utilities/applications
|
||||||
|
(T) Hard Drive (ATA) driver
|
||||||
|
(T) cdrom (ATAPI) driver
|
||||||
|
|
||||||
|
|
||||||
|
Change Log
|
||||||
|
----------
|
||||||
|
0.16
|
||||||
|
12/16/04 - initrd going to be loaded as a gzipped ext2 image (kernel module) - faster loading!
|
||||||
|
|
||||||
|
0.15
|
||||||
|
07/10/04 - Multiboot support added, loadable by GRUB
|
||||||
|
|
||||||
|
0.14
|
||||||
|
05/21/04 - C++ support in kernel, can use classes & templates
|
||||||
|
04/04/04 - video_line function for diagonal lines
|
||||||
|
03/16/04 - new VFS design with support for a loop device
|
||||||
|
03/01/04 - Thanks to Ben Meyer for helping me get a Makefile working and building on linux to work!
|
||||||
|
|
||||||
|
0.13
|
||||||
|
01/26/04 - functions added to read/write CMOS clock date and time
|
||||||
|
01/19/04 - fixed bug GDTR/IDTR pointing to physical rather than linear table base address
|
||||||
|
01/07/04 - fixed bug not reading sectors correctly from floppy
|
||||||
|
12/28/03 - fixed bug not storing eax on interrupt
|
||||||
|
12/25/03 - fixed bug in mm_palloc()
|
||||||
|
12/25/03 - incorporated output functions as regular functions rather than as part of a linked library
|
||||||
|
12/23/03 - re-written physical memory manager using bitmap instead of stack
|
||||||
|
12/22/03 - kernel relocated to 3gb linear / 1mb+24kb physical to allow for app. address space
|
||||||
|
|
||||||
|
0.12
|
||||||
|
12/21/03 - sample bmp loader tested, works (tests loading a kernel of size ~ 932kb)
|
||||||
|
12/20/03 - GDT/IDT now located at 1mb physical, before kernel
|
||||||
|
10/30/03 - turns floppy motor off
|
||||||
|
10/30/03 - keyboard LEDs working
|
||||||
|
10/29/03 - paging functions working
|
||||||
|
10/15/03 - physical memory management page allocators working
|
||||||
|
|
||||||
|
0.11
|
||||||
|
10/09/03 - PS/2 mouse driver
|
||||||
|
|
||||||
|
0.10
|
||||||
|
09/11/03 - Rewritten C and assembly kernel with VESA GUI mode support, keyboard driver
|
||||||
|
|
||||||
|
0.05
|
||||||
|
05/14/03 - HGUI24/HGUI32 commands finished for testing GUI on both 24bpp and 32bpp graphics cards
|
||||||
|
05/14/03 - first web release!
|
||||||
|
|
||||||
|
0.04
|
||||||
|
03/09/03 - added VM shortcut command
|
||||||
|
03/09/03 - press up to fill retrieve last inputted command for Nate Scholten
|
||||||
|
03/08/03 - press clear to clear console input
|
||||||
|
03/07/03 - added "shortcut" commands PC, IC, ? for Nate Scholten
|
||||||
|
03/06/03 - added PROMPTC, INPUTC commands
|
||||||
|
|
||||||
|
0.03
|
||||||
|
12/30/02 - Command Line Interface working, accepting basic commands
|
||||||
|
|
||||||
|
0.02
|
||||||
|
12/11/02 - Assembly bootsector can load stage2 ("console")
|
||||||
|
|
||||||
|
0.01
|
||||||
|
12/01/02 - Real mode assembly bootsector boots from floppy disk successfully
|
||||||
|
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 900 KiB |
@@ -0,0 +1,46 @@
|
|||||||
|
#!/usr/bin/env ruby
|
||||||
|
|
||||||
|
# Copyright (c) 2013-2022 Josh Holtrop
|
||||||
|
#
|
||||||
|
# MIT License
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining
|
||||||
|
# a copy of this software and associated documentation files (the
|
||||||
|
# "Software"), to deal in the Software without restriction, including
|
||||||
|
# without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
# distribute, sublicense, and/or sell copies of the Software, and to
|
||||||
|
# permit persons to whom the Software is furnished to do so, subject to
|
||||||
|
# the following conditions:
|
||||||
|
#
|
||||||
|
# The above copyright notice and this permission notice shall be
|
||||||
|
# included in all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||||
|
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||||
|
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||||
|
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
|
||||||
|
script = File.join(File.dirname(__FILE__), ".rscons-3.1.0-4892f95d56c30bbe9aa0c9964062ec5b.rb")
|
||||||
|
unless File.exist?(script)
|
||||||
|
if File.read(__FILE__, mode: "rb") =~ /^#==>(.*)/
|
||||||
|
require "zlib"
|
||||||
|
require "base64"
|
||||||
|
encoded_compressed = $1
|
||||||
|
unescaped_compressed = Base64.decode64(encoded_compressed)
|
||||||
|
inflated = Zlib::Inflate.inflate(unescaped_compressed)
|
||||||
|
File.open(script, "wb") do |fh|
|
||||||
|
fh.write(inflated)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
raise "Could not decompress."
|
||||||
|
end
|
||||||
|
end
|
||||||
|
load script
|
||||||
|
if __FILE__ == $0
|
||||||
|
Rscons::Cli.new.run(ARGV)
|
||||||
|
end
|
||||||
|
#==>eJztff13GzeS4O/5K9ptJyZlirIzN7PvMVYkW5IT7cmWT5KznqUZDkU2pR6T3dxuUrJW5v7thyp8VQHoJiXbuey+87yJ2PgoAIVCoVAoVD18sLUoi63zNNtKsquoWJzffFck/7FIiySKs2S+dTmfz2KbNEovknIefzfNR4tJEp2Uwzwrv4si9f0iK1PxFUXDyaAso+fPozKZjDElik4OTg/Oou0o/pB0n05jlTpKxtF1kc6TRpq3oo1pUpaDi6SpckV+3h/mk7wQFWVr7cFsNkmHg3maZ22ROxBtyiKmTjo21dpZOtkxGQycqrsjGrbNJdnI/M4Xc+huHIJLQA4XRZFkc9tNGCdtMikF7kaV+WrI7WQwvBTwo8/TzyQXW52207I/2Gmczos0u2iy7CiapFlSCsDTdikwM2/EH7K4FW0+C5bDZvrX6fyyn2aj5BO2CDmtKP3sVMC2IS96wPDA8vnwRUE23kAdidgn6xR0UbuqBp09rzkYiJcbqiEGlUbPFbrK9D+TaDN6tubg3cn1elFdYBUdrTVImPs1BummJJPSxc1wUCbR1Em8vkyyqCOm4Cea6lI4rvCf/vJs6nZFVr8okiT7aWX1Hyuq3ySTSX7906rqf6mofj5ZJLT3FdX/V0X1qViq2Xzw04rqf62oPrwZZGu0/reK6teXglOuRt2/VI09n4zo5IWqP6ueOKhOJ6+ietXEQXU6eRXVKydOVKeTV1G9auKgOp28iupVEwfV6eRVVK+aOKhOJ6+iet3ErSKbqlkrkjKZ19BMiM94/IF986/12GAF+6OgHCb0xfuianK65kjo7zRvS6FEwNBgbf6sSK8Gc91XkGCCsoTo4Mm7l3/vvz16cfbq+OR1tP1f0dZUdPT687S8KbdIPxoHb37rxmcHJ6/jXrQt5vPTPCmmcTP64Yfo++vuOB3n0fByUAyGIv10lgzTwaTXTrPhZDFKoNV2OR/M2+P5zYzITg5CRan5/GYnMGD9S/6F/8L/jdQn6McX+aSI98IKYwhhMJ8X/SIZjJIi6ojepldJ/2pQpINsXtoCg+FQTK4glc75Ip2M+qO0CGX60p1TIOvPL6Gx0m+7HBbpbB6qVKYTSavZOL1YFEmozFVSnOcKeTC/QlZK5wLnQhhQKNt1wYiVNC8WJtsMTKTj5J6c7h2/Oe2/fHd4tN/fPzwRE/35cxRjOb1wRcH2KJkkgvD88nped82oBeh383QiqgC1CHGlzxEiyirc9y+KfDGDCt0emWk5uPmg/NhPPiXDBUxjf3Ypdn5NJbuhTA9CscgaBRIGYr2FIMXKzEb9mSDaadmKysv8uo/JrWgwmeifSTY4nwhmpGlED3JPLPpEELgg62yYdOPxIJ0gz5pOBdQSVokeiuokbQ7mwknSRd0GRVE36TuzfP3CIAfDonzgwreLMTYkETNeMIQh9QPtu2PV9XFV9UeD+SDudWO3YtwjSzs0rHB7NasfiBrnT1Q+xR/tLLm22BBTuBA0PckHoz5ZXqRiG/IoJdCDFfTD7YZtWsJXJGJqYZIEJXMahnYs6CKZL4oseurBhJPiDZZWk9QYDwRLbNbRNl/G6dgjpHYynVEuKpA8FPCLHHiHQPdVWuTZNKGoFlDOBJQ2gurGYsUMFpM5m0DIJzltCVX2LQnNGWXtfhf1lokjzAZTcbqTWZ/dNk2JNdo0v556HCA8vYtsIpAS7Zp0TY4UNT7tuqfN5JPYScTEPGgE6Fzsqb83Pjz5vNncogJAYEF4lfV5uRXTmiVsr+OU171d1sC2+OZo4PIKz6Md32m0N5qPtkKlR2kJf1uRZuQwVaI/j561okc/shp+v7u0Us9wOg0U8bzJEM2FI9AU6O7C6ILDgoXrtWy5oUrpdrAProhmchVKoZPV41BAehxRkxI4NZDIGsCRAdxDIPRJmDMKW1atohDbpQsGORvsaloCiSJnjwvsDN4uGK4Z1PGoTKbmAX2dEnQflfOREHZbeLpvRfErBBmpWlHj4W0aPYmeLbce3rqtgYpk2ezELXXWaKEWwk61At2eLcRSQoFFVe3P837ZUB+umE2RNZwkg0zjF3Di4YZmCj5bXCRzze0pN4R0O3xZukimuZBSZWZD/rGdfyXGCn0u28W0P3ayyawjKCHuJcN5XqSJaVyIk/Po9vOgFZ1/js6lOun59s/RQKLNdk7Xvanon8lvmF+2l1nyaa65LfTY9ORmJ1RcLJjGflq0xS6FfbVFos2o+7j9uBU9brcf95ruZhdFUK2Yigq0kpj24SKJsnRSu23I8SC9eVMseNJ8/Wl2C7YhRWsE2ZQV44aVxZs+ZeFq5WeBkOAAS93Oes1et54wZxocESkA9kIlRw0W8xzKEMS7faw+v9GR1cglZnNWZcSSDkovBzax26Nb3RXdCMRnWwGqYKDhZsJslA3EGbjpt0UiacUmcsiAQCAnnX08A+G2sevKqefJRZqRcekZMXAFv6KiJxK+PBQfFAUoVH6OEjZtgjbFUZKLnlh3kApxMzSd/vFn3SNBSMr5zusKJ2Yf44sMW57o1XwHlougv4zjerws+SR4w45XDreT+ARgpdlF9PBWFljGsJDoAR7+3ZuNywH9T+PiHu5M3TD6vh3fV1TakIeUfAYELgVuPVBgiyFNEvx78MDNao+FyCOmSsuqEZcd4QQPP7Q4z85R0BJXmPiYY/nBtWvHoH61R6SkSjPdwd74uURk1VDGyXx42eioM2IroquY1PyY3ARqidQWNmVruEOdDEqBJY3HHtzeKiA1rELDUP018n6ftrX6IJGOA/NEQVG+KY/7nnRfS4JW7g/1HjHQ2BBMoWxFP5xP8uFHQn2QLMiqKOcV6mZ1JsNy5SQdJg8aT33eYunCAgQVoDlauso6OwHtaSL4Fa6RTiRXip6nTtTtkb5i3+1mBl9GTRaaPzwkaFgNb+iVC09uXjGrLtZ5luVzsYWKXxM4PUB2mkUDkXqZFLwx2bnYQ9PqRbh+21GWG0TBcNMsGQVanCdF/+oCZmYymJ6PBkijMlVVZpTKcuReIKjV7XRpdcU4uQ4mRWMcjjgGzfgZls9eBRDGe/nZ1ulR1+93j6x3u4dVHfOrVxzHI29InBr1+d09ua9x8vZnqdvtcRU2vY2xgm+FFOSRljNomf6Z9ESCGeYL0NlwjkGxJwtUoG8dXsUUzqq5baPUhH/qQgyu+94Qutb6iLEQPjWTxe5RzU46druObJbNh4YPZgRCJAiVB+FvJnb/ZbXWSK5OIhGjnO3Z9dQfXuiOcc+Zq5ypOjq/SZPJKOINr+rrGjpqKW6BvrXsaMyh9rWHkmVQXws/GduBhPYokS2JXQH2DtOSK9t9343EFJZibWTzcSP+fvPHfyljCVRuzUsUmDnEJaUHzHT1yfhNFcqcSYiNrT8HIRYUi5sPb23xJb+bFsPBPHFu+JiIPbW4cHiJgQTEuE0htRczME1xALoX1gYJLhr+CmiIH97qBpYxokL2pgoXq3iTPpLW71t3pWBNNx+y3+iyDq/MaEcs2sfhvOXjOOpEcbzsUKTda4HoPkWWQehGSIKRXqFTDfXVVJ2oYh31q8xXyeSZSDDHcnaps/IyhMCxo1OzyOHCZZ+piBpze73nrBavQW+1CJY2WSTexop5Ys24FzISXteCcXdct0KZzPukoUZFB7gWPmD7Febh8bvsY5ZfZxIU3DlHbGHqvQF3IuiVYjGKATRrFuz6dFB5CamOBXAqQhmy/t7OKX1vrZ2Cg8cLPJD77YqjVykO5N9fd42OUyowrZ7FPaIbvRU3yAibYLwclOmQKOkMqqzNQiN0rgZ2JAhGpAn2cprgZW9Dwu509g9evXh3dNYHG4Szk3d7Z4fHb/q/vTg5fPHy6OBUg8BZ8HH1QLfHytklInd2wRPKB978dnsNgTCnj1041HpFLwS1i/xA+baXxZvYbuB5WCyGUEMo5k1CHEh8NnAFlW6DgUwmz3yaAUWIwkUybsg/LZE6LwaQqAR5DVSmMLnQlHQNmlUH6s4A0D95gLRgQoKYPPzAZoyGIkl21UH7bTxsioMm/LdHutgOj4qA4YgQpDgo+4JxXOYjbfljbqU6DJhB3Ay63x9PBhdlA/9LDqf4Le9RHzg3qCpLcItkMmk8euafxosreqt7nRd44XYK5fFD3Q2zNsuP6cy5Q5RlQ/dtkMMu2+CiVAAgkxOAB/9ADRg8IEgi48dUgS8kZLqpFFdw4gL7ImIkQzOeWAp327gUkzBJVreBB4oJUmOEB4+JrysDMpkIUIikLt4f8h1MIYCp6QPmhQAFLiHhgF/RkERNF3sq/jvprRbfEDR0Da16NgfF8NI7MdX2XrUZ7+29Onrxy6mQK7sKDGKrFy59tL+6NMdACEvy1lv2HhbAw1vN8uK9t28F9357cvDq8L0Qbj3bAjnDXVXu8M0B9OURGRuFDZhRd/lfATka0peOGHtV3pRFns+/Rrc0qLXnrabGF83d4Zu9tSbu7YuzX2tmzYF6dPjy6PDN/14NWRSsIwYf7P7hyVpQV/QX5nOa5ZvDm4vrlD1gCMwXVGEYD8yQU8ZrS7Qyyq/LUEvrQ5lJW8tv213A+ea/DVoedjWYF6cazKNnxKCpDtqkGprt1NrQZtXQBKWuDQ4smIs57qI7jXiTHRdWo7UKzpM7wllzepJQBbl4vNIBfUFx5UmIRNB5wKUOLctUikIo24Uy8MNrqb64lGVmg7TArV/LyVyMEUIhE7HVvciLohjcNHGrhm2aJlKWKGpLOdsVQzhebbkqaSV0PhwtplrEkqJv/3JQXkbbRhKe5/1LvwCe0lAZ1z+/afzQAcukJtOEMM1iv5wXUhusr4Rupuf5pBntyDR5Eo5QZjb1gtZW8KakpUESCyqwI0CNiu4hSm7mjE0MrEJYWPdgJtXC6xshQ2FusaKuI/UpBv50475OjZ2VRTQrtFcUSPgtpmjJ3HAuSqYIQQJrsFwyc+LT07eINNZfB2B9X3Xlr6POqG6HtpHqVcuwjEkLine truncated
|
||||||
@@ -0,0 +1,235 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <ft2build.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include FT_FREETYPE_H
|
||||||
|
#include <string.h>
|
||||||
|
#include <strings.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
|
#define N_CHARS 128
|
||||||
|
|
||||||
|
#define round_up_26_6(val) (((val) + 63) >> 6u)
|
||||||
|
|
||||||
|
int max_advance;
|
||||||
|
int max_top = -9999;
|
||||||
|
int min_bottom = 9999;
|
||||||
|
int line_height;
|
||||||
|
int baseline_offset;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int width;
|
||||||
|
int height;
|
||||||
|
int top;
|
||||||
|
int left;
|
||||||
|
uint8_t * bitmap;
|
||||||
|
} char_info_t;
|
||||||
|
|
||||||
|
static char_info_t char_infos[N_CHARS];
|
||||||
|
|
||||||
|
static void load_char(FT_Face face, int char_code)
|
||||||
|
{
|
||||||
|
if (FT_Load_Char(face, char_code, FT_LOAD_RENDER) != 0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int advance = round_up_26_6(face->glyph->advance.x);
|
||||||
|
if (advance > max_advance)
|
||||||
|
{
|
||||||
|
max_advance = advance;
|
||||||
|
}
|
||||||
|
if ((face->glyph->bitmap.width == 0) ||
|
||||||
|
(face->glyph->bitmap.rows == 0))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
char_infos[char_code].width = face->glyph->bitmap.width;
|
||||||
|
char_infos[char_code].height = face->glyph->bitmap.rows;
|
||||||
|
char_infos[char_code].top = face->glyph->bitmap_top;
|
||||||
|
if (char_infos[char_code].top > max_top)
|
||||||
|
{
|
||||||
|
max_top = char_infos[char_code].top;
|
||||||
|
}
|
||||||
|
int bottom = char_infos[char_code].top - char_infos[char_code].height;
|
||||||
|
if (bottom < min_bottom)
|
||||||
|
{
|
||||||
|
min_bottom = bottom;
|
||||||
|
}
|
||||||
|
char_infos[char_code].left = face->glyph->bitmap_left;
|
||||||
|
char_infos[char_code].bitmap = malloc(char_infos[char_code].width * char_infos[char_code].height);
|
||||||
|
memcpy(char_infos[char_code].bitmap,
|
||||||
|
face->glyph->bitmap.buffer,
|
||||||
|
char_infos[char_code].width * char_infos[char_code].height);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void generate_bytes(FILE * file, const uint8_t * bytes, int count)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < count; i++)
|
||||||
|
{
|
||||||
|
if (i % 8 == 0)
|
||||||
|
{
|
||||||
|
fprintf(file, " ");
|
||||||
|
}
|
||||||
|
fprintf(file, "0x%02xu,", bytes[i]);
|
||||||
|
if ((i + 1) % 8 == 0)
|
||||||
|
{
|
||||||
|
fprintf(file, "\n");
|
||||||
|
}
|
||||||
|
else if (i < (count - 1))
|
||||||
|
{
|
||||||
|
fprintf(file, " ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (count % 8 != 0)
|
||||||
|
{
|
||||||
|
fprintf(file, "\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool generate(const char * d_file_name)
|
||||||
|
{
|
||||||
|
FILE * fh = fopen(d_file_name, "wb");
|
||||||
|
fprintf(fh, "module hulk.kfont;\n");
|
||||||
|
fprintf(fh, "struct CharInfo {\n");
|
||||||
|
fprintf(fh, " /** Glyph bitmap width. */\n");
|
||||||
|
fprintf(fh, " ubyte width;\n");
|
||||||
|
fprintf(fh, " /** Glyph bitmap height. */\n");
|
||||||
|
fprintf(fh, " ubyte height;\n");
|
||||||
|
fprintf(fh, " /** Glyph bitmap top offset (descent from top of text row). */\n");
|
||||||
|
fprintf(fh, " ubyte top;\n");
|
||||||
|
fprintf(fh, " /** Glyph bitmap left offset. */\n");
|
||||||
|
fprintf(fh, " ubyte left;\n");
|
||||||
|
fprintf(fh, " /** Glyph bitmap. */\n");
|
||||||
|
fprintf(fh, " const ubyte[] bitmap;\n");
|
||||||
|
fprintf(fh, "};\n");
|
||||||
|
fprintf(fh, "struct FontInfo {\n");
|
||||||
|
fprintf(fh, " /** Font line height. */\n");
|
||||||
|
fprintf(fh, " ubyte line_height;\n");
|
||||||
|
fprintf(fh, " /** Font advance. */\n");
|
||||||
|
fprintf(fh, " ubyte advance;\n");
|
||||||
|
fprintf(fh, " /** Characters. */\n");
|
||||||
|
fprintf(fh, " const CharInfo[] chars;\n");
|
||||||
|
fprintf(fh, "};\n");
|
||||||
|
fprintf(fh, "/** Kernel font data. */\n");
|
||||||
|
fprintf(fh, "__gshared const FontInfo Kfont = {\n");
|
||||||
|
fprintf(fh, " %du,\n", line_height);
|
||||||
|
fprintf(fh, " %du,\n", max_advance);
|
||||||
|
fprintf(fh, " [\n");
|
||||||
|
for (int i = 0; i < N_CHARS; i++)
|
||||||
|
{
|
||||||
|
fprintf(fh, " /* %d */", i);
|
||||||
|
fprintf(fh, " CharInfo(\n");
|
||||||
|
fprintf(fh, " %du /* width */,\n", char_infos[i].width);
|
||||||
|
fprintf(fh, " %du /* height */,\n", char_infos[i].height);
|
||||||
|
fprintf(fh, " %du /* top */,\n", char_infos[i].top);
|
||||||
|
fprintf(fh, " %du /* left */,\n", char_infos[i].left);
|
||||||
|
if (char_infos[i].width > 0)
|
||||||
|
{
|
||||||
|
fprintf(fh, " [\n");
|
||||||
|
generate_bytes(fh, char_infos[i].bitmap, char_infos[i].width * char_infos[i].height);
|
||||||
|
fprintf(fh, " ]\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
fprintf(fh, " []\n");
|
||||||
|
}
|
||||||
|
fprintf(fh, " ),\n");
|
||||||
|
}
|
||||||
|
fprintf(fh, " ],\n");
|
||||||
|
fprintf(fh, "};\n");
|
||||||
|
fclose(fh);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char * argv[])
|
||||||
|
{
|
||||||
|
/* Expect: font file, size, out file */
|
||||||
|
if (argc != 4)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Incorrect arguments\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
const char * font_file = argv[1];
|
||||||
|
int size = atoi(argv[2]);
|
||||||
|
const char * out_file = argv[3];
|
||||||
|
|
||||||
|
FT_Library ft_library;
|
||||||
|
if (FT_Init_FreeType(&ft_library) != 0)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Could not initialize freetype\n");
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
FT_Face face;
|
||||||
|
if (FT_New_Face(ft_library, font_file, 0, &face) != 0)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Could not open %s\n", font_file);
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
FT_Set_Pixel_Sizes(face, 0, size);
|
||||||
|
|
||||||
|
for (int i = 0; i < N_CHARS; i++)
|
||||||
|
{
|
||||||
|
load_char(face, i);
|
||||||
|
}
|
||||||
|
line_height = round_up_26_6(face->size->metrics.height);
|
||||||
|
baseline_offset = (line_height - (max_top - min_bottom)) / 2 - min_bottom;
|
||||||
|
|
||||||
|
if ((line_height < 1) || (line_height > 0xFF))
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error: invalid line_height %d\n", line_height);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if ((max_advance < 1) || (max_advance > 0xFF))
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error: invalid max_advance %d\n", max_advance);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < N_CHARS; i++)
|
||||||
|
{
|
||||||
|
if ((char_infos[i].width < 0) || (char_infos[i].width > max_advance))
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error: invalid character %d width: %d (max advance %d)\n", i, char_infos[i].width, max_advance);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if ((char_infos[i].height < 0) || (char_infos[i].height > line_height))
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error: invalid character %d height: %d (line height %d)\n", i, char_infos[i].height, line_height);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if ((char_infos[i].left < 0) || (char_infos[i].left >= max_advance))
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error: invalid character %d left: %d (max advance %d)\n", i, char_infos[i].left, max_advance);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
/* Adjust character top to be index downward from top of font box. */
|
||||||
|
char_infos[i].top = line_height - baseline_offset - char_infos[i].top;
|
||||||
|
if ((char_infos[i].top < 0) || (char_infos[i].top >= line_height))
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error: invalid character %d top: %d (line height %d)\n", i, char_infos[i].top, line_height);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if ((char_infos[i].left + char_infos[i].width) > max_advance)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error: character %d left (%d) + width (%d) > advance (%d)\n",
|
||||||
|
i, char_infos[i].left, char_infos[i].width, max_advance);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if ((char_infos[i].top + char_infos[i].height) > line_height)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error: character %d top (%d) + height (%d) > line height (%d)\n",
|
||||||
|
i, char_infos[i].top, char_infos[i].height, line_height);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!generate(out_file))
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,88 @@
|
|||||||
|
/**
|
||||||
|
* HELLO console support.
|
||||||
|
*/
|
||||||
|
module hello.console;
|
||||||
|
|
||||||
|
import uefi;
|
||||||
|
import hello.hello;
|
||||||
|
import core.stdc.stdarg;
|
||||||
|
import hulk.writef;
|
||||||
|
|
||||||
|
struct Console
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Write a character to the console.
|
||||||
|
*
|
||||||
|
* @param ch Character to write.
|
||||||
|
*/
|
||||||
|
public static void write(ubyte ch)
|
||||||
|
{
|
||||||
|
if (ch == '\n')
|
||||||
|
{
|
||||||
|
write('\r');
|
||||||
|
}
|
||||||
|
CHAR16[2] s16 = [ch, 0];
|
||||||
|
st.ConOut.OutputString(st.ConOut, &s16[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write a formatted string to the console.
|
||||||
|
*
|
||||||
|
* @param s Format string.
|
||||||
|
* @param args Variable arguments structure.
|
||||||
|
*/
|
||||||
|
public static void writef(string s, va_list args)
|
||||||
|
{
|
||||||
|
hulk.writef.writefv(function void(ubyte ch) {
|
||||||
|
Console.write(ch);
|
||||||
|
}, s, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write a formatted string to the console.
|
||||||
|
*
|
||||||
|
* @param s Format string.
|
||||||
|
*/
|
||||||
|
public static extern (C) void writef(string s, ...)
|
||||||
|
{
|
||||||
|
va_list args;
|
||||||
|
va_start(args, s);
|
||||||
|
writef(s, args);
|
||||||
|
va_end(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write a formatted string and newline to the console.
|
||||||
|
*
|
||||||
|
* @param s Format string.
|
||||||
|
*/
|
||||||
|
public static extern (C) void writefln(string s, ...)
|
||||||
|
{
|
||||||
|
va_list args;
|
||||||
|
va_start(args, s);
|
||||||
|
writef(s, args);
|
||||||
|
writef("\n", args);
|
||||||
|
va_end(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write a message to press any key and wait for a key to be pressed.
|
||||||
|
*/
|
||||||
|
public static void wait_key()
|
||||||
|
{
|
||||||
|
writefln("Press any key...");
|
||||||
|
st.ConIn.Reset(st.ConIn, FALSE);
|
||||||
|
EFI_INPUT_KEY key;
|
||||||
|
while (st.ConIn.ReadKeyStroke(st.ConIn, &key) == EFI_NOT_READY)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clear the console.
|
||||||
|
*/
|
||||||
|
public static void clear()
|
||||||
|
{
|
||||||
|
st.ConOut.ClearScreen(st.ConOut);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,579 @@
|
|||||||
|
/**
|
||||||
|
* HELLO, the HOS EFI Lightweight LOader.
|
||||||
|
*/
|
||||||
|
module hello.hello;
|
||||||
|
|
||||||
|
import uefi;
|
||||||
|
import hello.console;
|
||||||
|
import hulk.bootinfo;
|
||||||
|
import hulk.header;
|
||||||
|
import hulk.pagetable;
|
||||||
|
import hulk.cpu;
|
||||||
|
import hulk.memory;
|
||||||
|
import ldc.llvmasm;
|
||||||
|
import hulk.serial;
|
||||||
|
|
||||||
|
__gshared EFI_SYSTEM_TABLE * st;
|
||||||
|
/** HULK binary start address, 4KB-aligned. */
|
||||||
|
extern extern(C) __gshared ubyte _hulk_bin_start;
|
||||||
|
/** HULK binary end address, 4KB-aligned. */
|
||||||
|
extern extern(C) __gshared ubyte _hulk_bin_end;
|
||||||
|
private align(4096) __gshared ubyte[1024 * 1024] scratch_buffer;
|
||||||
|
/** Index to the memory map region being used to allocate page tables. */
|
||||||
|
private __gshared size_t memory_map_page_table_alloc_index;
|
||||||
|
|
||||||
|
private HulkHeader * hulk_header()
|
||||||
|
{
|
||||||
|
return cast(HulkHeader *)&_hulk_bin_start;
|
||||||
|
}
|
||||||
|
|
||||||
|
private BootInfo * bootinfo()
|
||||||
|
{
|
||||||
|
return &hulk_header().bootinfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
private ulong hulk_bin_phys()
|
||||||
|
{
|
||||||
|
return cast(ulong)&_hulk_bin_start;
|
||||||
|
}
|
||||||
|
|
||||||
|
private ulong hulk_bin_size()
|
||||||
|
{
|
||||||
|
return cast(ulong)&_hulk_bin_end - cast(ulong)&_hulk_bin_start;
|
||||||
|
}
|
||||||
|
|
||||||
|
private ulong hulk_bss_size()
|
||||||
|
{
|
||||||
|
return cast(ulong)hulk_header().hulk_bss_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
private ulong hulk_stack_size()
|
||||||
|
{
|
||||||
|
return hulk_header().stack_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
private ulong hulk_virt_base_address()
|
||||||
|
{
|
||||||
|
return hulk_header().virt_base;
|
||||||
|
}
|
||||||
|
|
||||||
|
private ulong hulk_virt_stack_top()
|
||||||
|
{
|
||||||
|
return hulk_header().virt_stack_top;
|
||||||
|
}
|
||||||
|
|
||||||
|
private ulong hulk_virt_framebuffer_address()
|
||||||
|
{
|
||||||
|
return hulk_header().virt_fb_buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Detect if we're running in QEMU.
|
||||||
|
*/
|
||||||
|
private bool in_qemu()
|
||||||
|
{
|
||||||
|
ulong * firmware_vendor = cast(ulong *) st.FirmwareVendor;
|
||||||
|
ulong fv1 = firmware_vendor[0];
|
||||||
|
return fv1 ==
|
||||||
|
((cast(ulong)'E') |
|
||||||
|
(cast(ulong)'D' << 16) |
|
||||||
|
(cast(ulong)'K' << 32) |
|
||||||
|
(cast(ulong)' ' << 48));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set a graphics mode.
|
||||||
|
*/
|
||||||
|
private bool set_graphics_mode()
|
||||||
|
{
|
||||||
|
uint max_horizontal_resolution = in_qemu() ? 1920u : 0xFFFFFFFFu;
|
||||||
|
UINTN buffer_size = scratch_buffer.sizeof;
|
||||||
|
EFI_GUID gop_guid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
|
||||||
|
EFI_HANDLE * handles = cast(EFI_HANDLE *)&scratch_buffer[0];
|
||||||
|
EFI_STATUS status = st.BootServices.LocateHandle(ByProtocol,
|
||||||
|
&gop_guid, null, &buffer_size, handles);
|
||||||
|
if (status != EFI_SUCCESS)
|
||||||
|
{
|
||||||
|
Console.writefln("LocateHandle: error %x", status);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
EFI_HANDLE gop_handle = handles[0];
|
||||||
|
EFI_HANDLE gop_interface;
|
||||||
|
status = st.BootServices.HandleProtocol(gop_handle,
|
||||||
|
&gop_guid, &gop_interface);
|
||||||
|
if (status != EFI_SUCCESS)
|
||||||
|
{
|
||||||
|
Console.writefln("HandleProtocol: error %x", status);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (gop_interface == null)
|
||||||
|
{
|
||||||
|
Console.writefln("null interface from HandleProtocol");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
EFI_GRAPHICS_OUTPUT_PROTOCOL * gop = cast(EFI_GRAPHICS_OUTPUT_PROTOCOL *)gop_interface;
|
||||||
|
UINTN info_size;
|
||||||
|
EFI_GRAPHICS_OUTPUT_MODE_INFORMATION * info;
|
||||||
|
uint best_width;
|
||||||
|
uint best_mode_number;
|
||||||
|
for (uint mode_number = 0u;
|
||||||
|
(status = gop.QueryMode(gop, mode_number, &info_size, &info)) == EFI_SUCCESS;
|
||||||
|
mode_number++)
|
||||||
|
{
|
||||||
|
if ((info.HorizontalResolution > best_width) &&
|
||||||
|
(info.HorizontalResolution <= max_horizontal_resolution))
|
||||||
|
{
|
||||||
|
best_width = info.HorizontalResolution;
|
||||||
|
best_mode_number = mode_number;
|
||||||
|
}
|
||||||
|
st.BootServices.FreePool(info);
|
||||||
|
}
|
||||||
|
if ((status = gop.SetMode(gop, best_mode_number)) != EFI_SUCCESS)
|
||||||
|
{
|
||||||
|
Console.writefln("SetMode: Error %x\n", status);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
bootinfo().fb.buffer = cast(uint *)gop.Mode.FrameBufferBase;
|
||||||
|
bootinfo().fb.width = gop.Mode.Info.HorizontalResolution;
|
||||||
|
bootinfo().fb.height = gop.Mode.Info.VerticalResolution;
|
||||||
|
bootinfo().fb.stride = gop.Mode.Info.PixelsPerScanLine;
|
||||||
|
bootinfo().fb.format = gop.Mode.Info.PixelFormat;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Walk the EFI memory map and translate it to the HULK bootinfo format.
|
||||||
|
*/
|
||||||
|
private void get_memory_map(ulong * physical_address_limit, UINTN * memory_map_key)
|
||||||
|
{
|
||||||
|
immutable static ubyte[] efi_to_hulk_memory_map_type = [
|
||||||
|
BootInfo.MemoryRegion.Type.Reserved, // EfiReservedMemoryType
|
||||||
|
BootInfo.MemoryRegion.Type.Bootloader, // EfiLoaderCode
|
||||||
|
BootInfo.MemoryRegion.Type.Bootloader, // EfiLoaderData
|
||||||
|
BootInfo.MemoryRegion.Type.Bootloader, // EfiBootServicesCode
|
||||||
|
BootInfo.MemoryRegion.Type.Bootloader, // EfiBootServicesData
|
||||||
|
BootInfo.MemoryRegion.Type.Reserved, // EfiRuntimeServicesCode
|
||||||
|
BootInfo.MemoryRegion.Type.Reserved, // EfiRuntimeServicesData
|
||||||
|
BootInfo.MemoryRegion.Type.Conventional, // EfiConventionalMemory
|
||||||
|
BootInfo.MemoryRegion.Type.Unusable, // EfiUnusableMemory
|
||||||
|
BootInfo.MemoryRegion.Type.ACPIReclaim, // EfiACPIReclaimMemory
|
||||||
|
BootInfo.MemoryRegion.Type.ACPINVS, // EfiACPIMemoryNVS
|
||||||
|
BootInfo.MemoryRegion.Type.MemoryMappedIO, // EfiMemoryMappedIO
|
||||||
|
BootInfo.MemoryRegion.Type.MemoryMappedIOPortSpace, // EfiMemoryMappedIOPortSpace
|
||||||
|
BootInfo.MemoryRegion.Type.PalCode, // EfiPalCode
|
||||||
|
];
|
||||||
|
*physical_address_limit = 0u;
|
||||||
|
UINTN memory_map_size = scratch_buffer.sizeof;
|
||||||
|
UINTN descriptor_size;
|
||||||
|
UINT32 descriptor_version;
|
||||||
|
ubyte * scratch_base = cast(ubyte *)&scratch_buffer[0];
|
||||||
|
UINTN status = st.BootServices.GetMemoryMap(
|
||||||
|
&memory_map_size,
|
||||||
|
cast(EFI_MEMORY_DESCRIPTOR *)scratch_base,
|
||||||
|
memory_map_key,
|
||||||
|
&descriptor_size,
|
||||||
|
&descriptor_version);
|
||||||
|
if (status != EFI_SUCCESS)
|
||||||
|
{
|
||||||
|
Console.writefln("GetMemoryMap: Error %x", status);
|
||||||
|
for (;;) {}
|
||||||
|
}
|
||||||
|
size_t n_entries = memory_map_size / descriptor_size;
|
||||||
|
size_t count;
|
||||||
|
bool found_bss;
|
||||||
|
bool found_stack;
|
||||||
|
bool found_fb_buffer1;
|
||||||
|
const(size_t) fb_size = (bootinfo().fb.stride * bootinfo().fb.height * 4u + PAGE_SIZE - 1u) & ~(PAGE_SIZE - 1u);
|
||||||
|
for (count = 0u; count < n_entries; count++)
|
||||||
|
{
|
||||||
|
if (count > bootinfo().memory_map.length)
|
||||||
|
{
|
||||||
|
Console.writefln("Memory map too large");
|
||||||
|
for (;;) {}
|
||||||
|
}
|
||||||
|
EFI_MEMORY_DESCRIPTOR * descriptor = cast(EFI_MEMORY_DESCRIPTOR *)&scratch_base[count * descriptor_size];
|
||||||
|
ulong end_address = descriptor.PhysicalStart + descriptor.NumberOfPages * 4096u;
|
||||||
|
if ((end_address > *physical_address_limit) &&
|
||||||
|
((descriptor.Type == EfiLoaderCode) ||
|
||||||
|
(descriptor.Type == EfiLoaderData) ||
|
||||||
|
(descriptor.Type == EfiBootServicesCode) ||
|
||||||
|
(descriptor.Type == EfiBootServicesData) ||
|
||||||
|
(descriptor.Type == EfiRuntimeServicesCode) ||
|
||||||
|
(descriptor.Type == EfiRuntimeServicesData) ||
|
||||||
|
(descriptor.Type == EfiConventionalMemory)))
|
||||||
|
{
|
||||||
|
*physical_address_limit = end_address;
|
||||||
|
}
|
||||||
|
if (descriptor.Type >= efi_to_hulk_memory_map_type.length)
|
||||||
|
{
|
||||||
|
Serial.writefln("Unexpected descriptor type %u", descriptor.Type);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
bootinfo().memory_map[count].base = descriptor.PhysicalStart;
|
||||||
|
bootinfo().memory_map[count].size = descriptor.NumberOfPages * 4096u;
|
||||||
|
bootinfo().memory_map[count].type = efi_to_hulk_memory_map_type[descriptor.Type];
|
||||||
|
if ((!found_bss) &&
|
||||||
|
(descriptor.Type == EfiConventionalMemory) &&
|
||||||
|
(bootinfo().memory_map[count].size >= hulk_bss_size()))
|
||||||
|
{
|
||||||
|
bootinfo().bss_phys = bootinfo().memory_map[count].base;
|
||||||
|
bootinfo().memory_map[count].base += hulk_bss_size();
|
||||||
|
bootinfo().memory_map[count].size -= hulk_bss_size();
|
||||||
|
Serial.writefln("Locating HULK BSS at %x:%x",
|
||||||
|
bootinfo().bss_phys,
|
||||||
|
bootinfo().bss_phys + hulk_bss_size() - 1u);
|
||||||
|
found_bss = true;
|
||||||
|
}
|
||||||
|
if ((!found_stack) &&
|
||||||
|
(descriptor.Type == EfiConventionalMemory) &&
|
||||||
|
(bootinfo().memory_map[count].size >= hulk_stack_size()))
|
||||||
|
{
|
||||||
|
bootinfo().stack_phys = bootinfo().memory_map[count].base;
|
||||||
|
bootinfo().memory_map[count].base += hulk_stack_size();
|
||||||
|
bootinfo().memory_map[count].size -= hulk_stack_size();
|
||||||
|
Serial.writefln("Locating HULK stack at %x:%x",
|
||||||
|
bootinfo().stack_phys,
|
||||||
|
bootinfo().stack_phys + hulk_stack_size() - 1u);
|
||||||
|
found_stack = true;
|
||||||
|
}
|
||||||
|
if ((!found_fb_buffer1) &&
|
||||||
|
(descriptor.Type == EfiConventionalMemory) &&
|
||||||
|
(bootinfo().memory_map[count].size >= fb_size))
|
||||||
|
{
|
||||||
|
bootinfo().fb_buffer1_phys = bootinfo().memory_map[count].base;
|
||||||
|
bootinfo().memory_map[count].base += fb_size;
|
||||||
|
bootinfo().memory_map[count].size -= fb_size;
|
||||||
|
Serial.writefln("Locating HULK FB1 at %x:%x",
|
||||||
|
bootinfo().fb_buffer1_phys,
|
||||||
|
bootinfo().fb_buffer1_phys + fb_size - 1u);
|
||||||
|
found_fb_buffer1 = true;
|
||||||
|
}
|
||||||
|
Serial.writefln("Memory range %x:%x (%u)",
|
||||||
|
bootinfo().memory_map[count].base,
|
||||||
|
bootinfo().memory_map[count].base + bootinfo().memory_map[count].size - 1u,
|
||||||
|
bootinfo().memory_map[count].type);
|
||||||
|
}
|
||||||
|
bootinfo().memory_map_count = count;
|
||||||
|
if ((!found_bss) || (!found_stack) || (!found_fb_buffer1))
|
||||||
|
{
|
||||||
|
for (;;) {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void * alloc_pt_page()
|
||||||
|
{
|
||||||
|
BootInfo.MemoryRegion * regions = &bootinfo().memory_map[0];
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
BootInfo.MemoryRegion * region = &bootinfo().memory_map[memory_map_page_table_alloc_index];
|
||||||
|
if ((region.type == BootInfo.MemoryRegion.Type.Conventional) && (region.size >= PAGE_SIZE))
|
||||||
|
{
|
||||||
|
void * addr = cast(void *)region.base;
|
||||||
|
region.base += PAGE_SIZE;
|
||||||
|
region.size -= PAGE_SIZE;
|
||||||
|
return addr;
|
||||||
|
}
|
||||||
|
memory_map_page_table_alloc_index++;
|
||||||
|
if (memory_map_page_table_alloc_index >= bootinfo().memory_map_count)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Allocate a new page table.
|
||||||
|
*/
|
||||||
|
private PageTable * new_page_table()
|
||||||
|
{
|
||||||
|
PageTable * pt = cast(PageTable *)alloc_pt_page();
|
||||||
|
if (pt == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
memset64(pt, 0u, 512);
|
||||||
|
return pt;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Map a virtual address to a physical address using 4KB pages.
|
||||||
|
*
|
||||||
|
* @param source_page Source page address.
|
||||||
|
* @param dest_page Destination page address.
|
||||||
|
* @param pt_base Page table base address.
|
||||||
|
*/
|
||||||
|
private bool map4k(ulong source_page, ulong dest_page, PageTable * pt_base)
|
||||||
|
{
|
||||||
|
PageTable * pt = pt_base;
|
||||||
|
for (size_t level = 0; level < 4u; level++)
|
||||||
|
{
|
||||||
|
PageTableEntry * ppte = pt.entry(source_page, level);
|
||||||
|
PageTableEntry pte = *ppte;
|
||||||
|
if (pte.present)
|
||||||
|
{
|
||||||
|
pt = pte.follow();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
PageTable * next_pt;
|
||||||
|
ulong addr;
|
||||||
|
if (level < 3u)
|
||||||
|
{
|
||||||
|
next_pt = new_page_table();
|
||||||
|
if (next_pt == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
addr = cast(ulong)next_pt;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
addr = dest_page;
|
||||||
|
}
|
||||||
|
*ppte = PageTableEntry(addr, PT_WRITABLE | PT_PRESENT);
|
||||||
|
pt = next_pt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Map a virtual region to a physical region using 4KB pages.
|
||||||
|
*
|
||||||
|
* @param source Source address.
|
||||||
|
* @param dest Destination address.
|
||||||
|
* @param size Region size.
|
||||||
|
* @param pt_base Page table base address.
|
||||||
|
*/
|
||||||
|
private bool map4kregion(ulong source, ulong dest, size_t size, PageTable * pt_base)
|
||||||
|
{
|
||||||
|
ulong end = source + size;
|
||||||
|
while (source < end)
|
||||||
|
{
|
||||||
|
if (!map4k(source, dest, pt_base))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
source += 4096u;
|
||||||
|
dest += 4096u;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Map a virtual address to a physical address using 2MB pages.
|
||||||
|
*
|
||||||
|
* @param source_page Source page address.
|
||||||
|
* @param dest_page Destination page address.
|
||||||
|
* @param pt_base Page table base address.
|
||||||
|
*/
|
||||||
|
private bool map2m(ulong source_page, ulong dest_page, PageTable * pt_base)
|
||||||
|
{
|
||||||
|
PageTable * pt = pt_base;
|
||||||
|
for (size_t level = 0; level < 3u; level++)
|
||||||
|
{
|
||||||
|
PageTableEntry * ppte = pt.entry(source_page, level);
|
||||||
|
PageTableEntry pte = *ppte;
|
||||||
|
if (pte.present)
|
||||||
|
{
|
||||||
|
pt = pte.follow();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
PageTable * next_pt;
|
||||||
|
if (level < 2u)
|
||||||
|
{
|
||||||
|
next_pt = new_page_table();
|
||||||
|
if (next_pt == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
*ppte = PageTableEntry(next_pt, PT_WRITABLE | PT_PRESENT);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
*ppte = PageTableEntry(dest_page, PT_HUGE_PAGE | PT_WRITABLE | PT_PRESENT);
|
||||||
|
}
|
||||||
|
pt = next_pt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Map HULK virtual addresses to physical kernel location.
|
||||||
|
*
|
||||||
|
* @param pt_base Page table base address.
|
||||||
|
*/
|
||||||
|
private bool map_hulk(PageTable * pt_base)
|
||||||
|
{
|
||||||
|
/* Map HULK bin region. */
|
||||||
|
ulong virt = hulk_virt_base_address();
|
||||||
|
if (!map4kregion(virt, hulk_bin_phys(), hulk_bin_size(), pt_base))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
/* Map HULK bss region. */
|
||||||
|
virt += hulk_bin_size();
|
||||||
|
if (!map4kregion(virt, bootinfo().bss_phys, hulk_bss_size(), pt_base))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
/* Zero BSS region. */
|
||||||
|
memset64(cast(void *)bootinfo().bss_phys, 0u, hulk_bss_size() / 8);
|
||||||
|
/* Map HULK stack. */
|
||||||
|
virt = hulk_virt_stack_top() - hulk_stack_size();
|
||||||
|
if (!map4kregion(virt, bootinfo().stack_phys, hulk_stack_size(), pt_base))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build page tables in preparation to jump to HULK.
|
||||||
|
*
|
||||||
|
* @param physical_address_limit Maximum physical address to identity map.
|
||||||
|
*/
|
||||||
|
private bool build_page_tables(ulong physical_address_limit)
|
||||||
|
{
|
||||||
|
PageTable * pt_base = new_page_table();
|
||||||
|
if (pt_base == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
/* Map physical RAM. */
|
||||||
|
for (size_t addr = 0u; addr < physical_address_limit; addr += (2u * 1024u * 1024u))
|
||||||
|
{
|
||||||
|
if (!map2m(addr, addr, pt_base))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* Map any memory regions that are outside physical RAM. */
|
||||||
|
for (size_t i = 0u; i < bootinfo().memory_map_count; i++)
|
||||||
|
{
|
||||||
|
ulong addr = bootinfo().memory_map[i].base;
|
||||||
|
if (addr >= physical_address_limit)
|
||||||
|
{
|
||||||
|
if (!map4kregion(addr, addr, bootinfo().memory_map[i].size, pt_base))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* Map graphics framebuffer. */
|
||||||
|
ulong framebuffer_size = bootinfo().fb.height * bootinfo().fb.stride * 4u;
|
||||||
|
ulong fb_phys = cast(ulong)bootinfo().fb.buffer;
|
||||||
|
ulong fb_virt = hulk_virt_framebuffer_address();
|
||||||
|
if (!map4kregion(fb_virt, fb_phys, framebuffer_size, pt_base))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
/* Map HULK regions. */
|
||||||
|
if (!map_hulk(pt_base))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
/* Switch to the new page table. */
|
||||||
|
write_cr3(cast(ulong)pt_base);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Jump to HULK entry point.
|
||||||
|
*/
|
||||||
|
private void jump_to_hulk()
|
||||||
|
{
|
||||||
|
__asm(
|
||||||
|
"jmpq *$0",
|
||||||
|
"r,{rsp}",
|
||||||
|
hulk_header().entry, hulk_virt_stack_top());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find the ACPI XSDT.
|
||||||
|
*
|
||||||
|
* @return Whether it was found.
|
||||||
|
*/
|
||||||
|
private bool find_acpi_xsdt()
|
||||||
|
{
|
||||||
|
for (size_t i = 0u; i < st.NumberOfTableEntries; i++)
|
||||||
|
{
|
||||||
|
if ((st.ConfigurationTable[i].VendorGuid == EFI_ACPI_TABLE_GUID) ||
|
||||||
|
(st.ConfigurationTable[i].VendorGuid == ACPI_TABLE_GUID))
|
||||||
|
{
|
||||||
|
enum ulong rsdp_magic = 0x20525450_20445352u;
|
||||||
|
const(EFI_ACPI_2_0_ROOT_SYSTEM_DESCRIPTION_POINTER) * rdsp = cast(const(EFI_ACPI_2_0_ROOT_SYSTEM_DESCRIPTION_POINTER) *)st.ConfigurationTable[i].VendorTable;
|
||||||
|
if (rdsp.Signature == rsdp_magic)
|
||||||
|
{
|
||||||
|
if (rdsp.Revision >= EFI_ACPI_2_0_ROOT_SYSTEM_DESCRIPTION_POINTER_REVISION)
|
||||||
|
{
|
||||||
|
bootinfo().acpi_xsdt_phys = rdsp.XsdtAddress;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* EFI application entry point.
|
||||||
|
*
|
||||||
|
* @param image_handle EFI application handle.
|
||||||
|
* @param st Pointer to EFI system table.
|
||||||
|
*/
|
||||||
|
extern (C) EFI_STATUS efi_main(EFI_HANDLE image_handle, EFI_SYSTEM_TABLE * st)
|
||||||
|
{
|
||||||
|
.st = st;
|
||||||
|
|
||||||
|
Console.clear();
|
||||||
|
|
||||||
|
Console.writefln("Welcome to HELLO, HOS EFI Lightweight LOader, v0.1.0");
|
||||||
|
Console.writefln("Firmware vendor: '%S', version: 0x%x", st.FirmwareVendor, st.FirmwareVendor);
|
||||||
|
|
||||||
|
if (!find_acpi_xsdt())
|
||||||
|
{
|
||||||
|
Console.writefln("Error: Could not locate ACPI XSDT");
|
||||||
|
Console.wait_key();
|
||||||
|
return EFI_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!set_graphics_mode())
|
||||||
|
{
|
||||||
|
Console.wait_key();
|
||||||
|
return EFI_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
ulong physical_address_limit;
|
||||||
|
UINTN memory_map_key;
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
get_memory_map(&physical_address_limit, &memory_map_key);
|
||||||
|
|
||||||
|
EFI_STATUS status = st.BootServices.ExitBootServices(image_handle, memory_map_key);
|
||||||
|
if (status == EFI_INVALID_PARAMETER)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (status == EFI_SUCCESS)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
Console.writefln("ExitBootServices: Error %x", status);
|
||||||
|
Console.wait_key();
|
||||||
|
return EFI_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!build_page_tables(physical_address_limit))
|
||||||
|
{
|
||||||
|
return EFI_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
bootinfo().hulk_phys = hulk_bin_phys();
|
||||||
|
|
||||||
|
jump_to_hulk();
|
||||||
|
|
||||||
|
return EFI_SUCCESS;
|
||||||
|
}
|
||||||
+126
@@ -0,0 +1,126 @@
|
|||||||
|
/**
|
||||||
|
* ACPI (Advanced Configuration and Power Interface) functionality.
|
||||||
|
*/
|
||||||
|
module hulk.acpi;
|
||||||
|
|
||||||
|
import hulk.hurl;
|
||||||
|
import hulk.klog;
|
||||||
|
import hulk.memory;
|
||||||
|
import hulk.hurl.a1;
|
||||||
|
|
||||||
|
struct Acpi
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* List of ACPI tables.
|
||||||
|
*/
|
||||||
|
private static __gshared Header *[] tables;
|
||||||
|
|
||||||
|
enum uint XSDT_SIGNATURE = signature("XSDT");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ACPI table header structure.
|
||||||
|
*/
|
||||||
|
public static struct Header
|
||||||
|
{
|
||||||
|
uint signature;
|
||||||
|
uint length;
|
||||||
|
ubyte revision;
|
||||||
|
ubyte checksum;
|
||||||
|
char[6] oemid;
|
||||||
|
char[8] oemtableid;
|
||||||
|
uint oem_revision;
|
||||||
|
uint creator_id;
|
||||||
|
uint creator_revision;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* XSDT table.
|
||||||
|
*/
|
||||||
|
static struct XSDT
|
||||||
|
{
|
||||||
|
Header header;
|
||||||
|
/* Table pointers are not ulong-aligned! They begin at offset 36. */
|
||||||
|
align(4) ulong[1] tables;
|
||||||
|
}
|
||||||
|
static assert(XSDT.tables.offsetof == 36);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize ACPI.
|
||||||
|
*/
|
||||||
|
public static void initialize(ulong acpi_xsdt_phys)
|
||||||
|
{
|
||||||
|
Klog.writefln("\a3Initializing ACPI");
|
||||||
|
|
||||||
|
/* Map the XSDT header. */
|
||||||
|
map_table(acpi_xsdt_phys, PAGE_SIZE);
|
||||||
|
const(XSDT) * xsdt = cast(const(XSDT) *)acpi_xsdt_phys;
|
||||||
|
if (xsdt.header.signature != XSDT_SIGNATURE)
|
||||||
|
{
|
||||||
|
Klog.fatal_error("XSDT signature invalid");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
/* Map the entire XSDT. */
|
||||||
|
if (xsdt.header.length > PAGE_SIZE)
|
||||||
|
{
|
||||||
|
map_table(acpi_xsdt_phys, xsdt.header.length);
|
||||||
|
}
|
||||||
|
size_t n_entries = (xsdt.header.length - xsdt.header.sizeof) / xsdt.tables[0].sizeof;
|
||||||
|
Header ** table_pointers = cast(Header **)A1.allocate(n_entries * (Header *).sizeof);
|
||||||
|
tables = table_pointers[0..n_entries];
|
||||||
|
for (size_t i = 0u; i < n_entries; i++)
|
||||||
|
{
|
||||||
|
ulong address = xsdt.tables[i];
|
||||||
|
map_table(address, PAGE_SIZE);
|
||||||
|
Header * header = cast(Header *)address;
|
||||||
|
uint length = header.length;
|
||||||
|
if (length > PAGE_SIZE)
|
||||||
|
{
|
||||||
|
map_table(address, length);
|
||||||
|
}
|
||||||
|
tables[i] = header;
|
||||||
|
uint signature = header.signature;
|
||||||
|
Klog.writefln("Found ACPI table '%c%c%c%c' at %p",
|
||||||
|
signature & 0xFFu,
|
||||||
|
(signature >> 8u) & 0xFFu,
|
||||||
|
(signature >> 16u) & 0xFFu,
|
||||||
|
(signature >> 24u) & 0xFFu,
|
||||||
|
address);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get pointer to ACPI table by name.
|
||||||
|
*
|
||||||
|
* @param name
|
||||||
|
* Table name.
|
||||||
|
*/
|
||||||
|
public static Header * get_table(string name)
|
||||||
|
{
|
||||||
|
uint signature = signature(name);
|
||||||
|
foreach (table; tables)
|
||||||
|
{
|
||||||
|
if (table.signature == signature)
|
||||||
|
{
|
||||||
|
return table;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Klog.fatal_error("Could not find requested ACPI table");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Map an ACPI table.
|
||||||
|
*/
|
||||||
|
private static void map_table(ulong address, ulong length)
|
||||||
|
{
|
||||||
|
Hurl.identity_map_range(address, length, PT_WRITABLE | PT_DISABLE_CACHE | PT_NO_EXECUTE);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert table signature from string to 32-bit unsigned integer.
|
||||||
|
*/
|
||||||
|
private static uint signature(string s)
|
||||||
|
{
|
||||||
|
return s[0] | (s[1] << 8) | (s[2] << 16) | (s[3] << 24);
|
||||||
|
}
|
||||||
|
}
|
||||||
+382
@@ -0,0 +1,382 @@
|
|||||||
|
/**
|
||||||
|
* APIC (Advanced Programmable Interrupt Controller) functionality.
|
||||||
|
*/
|
||||||
|
module hulk.apic;
|
||||||
|
|
||||||
|
import hulk.hurl;
|
||||||
|
import hulk.acpi;
|
||||||
|
import hulk.him;
|
||||||
|
import hulk.rtc;
|
||||||
|
import hulk.pit;
|
||||||
|
import hulk.klog;
|
||||||
|
import hulk.memory;
|
||||||
|
|
||||||
|
struct Apic
|
||||||
|
{
|
||||||
|
private enum DEBUG = false;
|
||||||
|
private enum DEFAULT_IO_APIC_ADDRESS = 0xFEC0_0000u;
|
||||||
|
private enum uint PERIODIC_MODE = 0x2_0000u;
|
||||||
|
|
||||||
|
private enum ulong IRQ_PIT = 0u;
|
||||||
|
private enum ulong IRQ_RTC = 8u;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* MADT ACPI table structure.
|
||||||
|
*/
|
||||||
|
static struct MADT
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* MADT table entry header.
|
||||||
|
*/
|
||||||
|
static struct Entry
|
||||||
|
{
|
||||||
|
enum PROCESSOR_LOCAL_APIC = 0;
|
||||||
|
enum IO_APIC = 1;
|
||||||
|
enum IO_APIC_INTERRUPT_SOURCE_OVERRIDE = 2;
|
||||||
|
enum IO_APIC_NMI_SOURCE = 3;
|
||||||
|
enum LOCAL_APIC_NMI = 4;
|
||||||
|
enum LOCAL_APIC_ADDRESS_OVERRIDE = 5;
|
||||||
|
enum PROCESSOR_LOCAL_X2APIC = 9;
|
||||||
|
|
||||||
|
ubyte type;
|
||||||
|
ubyte length;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Processor Local APIC.
|
||||||
|
*/
|
||||||
|
static struct Entry0
|
||||||
|
{
|
||||||
|
Entry header;
|
||||||
|
ubyte acpi_processor_id;
|
||||||
|
ubyte apic_id;
|
||||||
|
uint flags;
|
||||||
|
}
|
||||||
|
static assert(Entry0.sizeof == 8);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* I/O APIC.
|
||||||
|
*/
|
||||||
|
static struct Entry1
|
||||||
|
{
|
||||||
|
Entry header;
|
||||||
|
ubyte io_apic_id;
|
||||||
|
ubyte _reserved;
|
||||||
|
uint io_apic_address;
|
||||||
|
uint global_system_interrupt_base;
|
||||||
|
}
|
||||||
|
static assert(Entry1.sizeof == 12);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* IO/APIC Interrupt Source Override.
|
||||||
|
*/
|
||||||
|
static struct Entry2
|
||||||
|
{
|
||||||
|
Entry header;
|
||||||
|
ubyte bus_source;
|
||||||
|
ubyte irq_source;
|
||||||
|
uint global_system_interrupt;
|
||||||
|
ushort flags;
|
||||||
|
ushort _reserved;
|
||||||
|
}
|
||||||
|
static assert(Entry2.sizeof == 12);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* IO/APIC Non-maskable interrupt source.
|
||||||
|
*/
|
||||||
|
static struct Entry3
|
||||||
|
{
|
||||||
|
Entry header;
|
||||||
|
ubyte nmi_source;
|
||||||
|
ubyte _reserved;
|
||||||
|
ushort flags;
|
||||||
|
ubyte[4] global_system_interrupt;
|
||||||
|
}
|
||||||
|
static assert(Entry3.sizeof == 10);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Local APIC Non-maskable interrupts.
|
||||||
|
*/
|
||||||
|
static struct Entry4
|
||||||
|
{
|
||||||
|
Entry header;
|
||||||
|
ubyte acpi_processor_id;
|
||||||
|
ubyte[2] flags;
|
||||||
|
ubyte lint_nr;
|
||||||
|
}
|
||||||
|
static assert(Entry4.sizeof == 6);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Local APIC Address Override.
|
||||||
|
*/
|
||||||
|
static struct Entry5
|
||||||
|
{
|
||||||
|
Entry header;
|
||||||
|
ushort _reserved;
|
||||||
|
ubyte[8] local_apic_address;
|
||||||
|
}
|
||||||
|
static assert(Entry5.sizeof == 12);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Processor Local x2APIC.
|
||||||
|
*/
|
||||||
|
static struct Entry9
|
||||||
|
{
|
||||||
|
Entry header;
|
||||||
|
ushort _reserved;
|
||||||
|
uint local_x2apic_id;
|
||||||
|
uint flags;
|
||||||
|
uint acpi_id;
|
||||||
|
}
|
||||||
|
static assert(Entry9.sizeof == 16);
|
||||||
|
|
||||||
|
Acpi.Header header;
|
||||||
|
uint local_apic_address;
|
||||||
|
uint flags;
|
||||||
|
|
||||||
|
static assert(MADT.sizeof == 0x2C);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Scan the MADT table.
|
||||||
|
*/
|
||||||
|
public void scan()
|
||||||
|
{
|
||||||
|
apic_registers = cast(ApicRegisters *)local_apic_address;
|
||||||
|
const(void) * end = cast(const(void) *)(&this) + header.length;
|
||||||
|
const(Entry) * entry = cast(const(Entry) *)(cast(ulong)&this + MADT.sizeof);
|
||||||
|
bool first_io_apic = true;
|
||||||
|
size_t n_cpus = 0u;
|
||||||
|
while (entry < end)
|
||||||
|
{
|
||||||
|
if (DEBUG)
|
||||||
|
{
|
||||||
|
Klog.writefln("MADT entry type %u, length %u", entry.type, entry.length);
|
||||||
|
}
|
||||||
|
switch (entry.type)
|
||||||
|
{
|
||||||
|
case Entry.PROCESSOR_LOCAL_APIC:
|
||||||
|
Entry0 * e = cast(Entry0 *)entry;
|
||||||
|
n_cpus++;
|
||||||
|
if (DEBUG)
|
||||||
|
{
|
||||||
|
Klog.writefln(" ACPI processor ID: %u, APIC ID: %u, Flags: 0x%x",
|
||||||
|
e.acpi_processor_id, e.apic_id, e.flags);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Entry.IO_APIC:
|
||||||
|
Entry1 * e = cast(Entry1 *)entry;
|
||||||
|
if (first_io_apic)
|
||||||
|
{
|
||||||
|
io_apic_registers = cast(IoApicRegisters *)e.io_apic_address;
|
||||||
|
first_io_apic = false;
|
||||||
|
}
|
||||||
|
if (DEBUG)
|
||||||
|
{
|
||||||
|
Klog.writefln(" I/O APIC ID: %u, I/O APIC Address: 0x%x, GSIB: %u",
|
||||||
|
e.io_apic_id, e.io_apic_address, e.global_system_interrupt_base);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Entry.IO_APIC_INTERRUPT_SOURCE_OVERRIDE:
|
||||||
|
Entry2 * e = cast(Entry2 *)entry;
|
||||||
|
if (e.global_system_interrupt < INT_APIC_COUNT)
|
||||||
|
{
|
||||||
|
irq_redirects[e.irq_source] = cast(ubyte)e.global_system_interrupt;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Klog.writefln("Warning: unexpected interrupt source override global system interrupt value: %u", e.global_system_interrupt);
|
||||||
|
}
|
||||||
|
if (DEBUG)
|
||||||
|
{
|
||||||
|
Klog.writefln(" Bus %u IRQ %u GSI %u Flags: 0x%x",
|
||||||
|
e.bus_source, e.irq_source, e.global_system_interrupt, e.flags);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Entry.IO_APIC_NMI_SOURCE:
|
||||||
|
Entry3 * e = cast(Entry3 *)entry;
|
||||||
|
if (DEBUG)
|
||||||
|
{
|
||||||
|
uint global_system_interrupt;
|
||||||
|
memcpy(&global_system_interrupt, &e.global_system_interrupt, 4u);
|
||||||
|
Klog.writefln(" NMI Source: %u, Flags: 0x%x, GSI: %u",
|
||||||
|
e.nmi_source, e.flags, global_system_interrupt);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Entry.LOCAL_APIC_NMI:
|
||||||
|
Entry4 * e = cast(Entry4 *)entry;
|
||||||
|
if (DEBUG)
|
||||||
|
{
|
||||||
|
ushort flags;
|
||||||
|
memcpy(&flags, &e.flags, 2u);
|
||||||
|
Klog.writefln(" ACPI Processor ID: %u, Flags: 0x%x, LINT#: %u",
|
||||||
|
e.acpi_processor_id, flags, e.lint_nr);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Entry.LOCAL_APIC_ADDRESS_OVERRIDE:
|
||||||
|
Entry5 * e = cast(Entry5 *)entry;
|
||||||
|
if (DEBUG)
|
||||||
|
{
|
||||||
|
memcpy(&apic_registers, &e.local_apic_address, 8u);
|
||||||
|
Klog.writefln(" Local APIC Address: 0x%x", apic_registers);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Entry.PROCESSOR_LOCAL_X2APIC:
|
||||||
|
Entry9 * e = cast(Entry9 *)entry;
|
||||||
|
if (DEBUG)
|
||||||
|
{
|
||||||
|
Klog.writefln(" x2APIC ID: %u, Flags: 0x%x, ACPI ID: %u",
|
||||||
|
e.local_x2apic_id, e.flags, e.acpi_id);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Move to next entry. */
|
||||||
|
entry = cast(const(Entry) *)(cast(size_t)entry + entry.length);
|
||||||
|
}
|
||||||
|
if (n_cpus > 0u)
|
||||||
|
{
|
||||||
|
Klog.writefln("%u CPU(s) found", n_cpus);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* APIC register structure.
|
||||||
|
*/
|
||||||
|
static struct ApicRegister
|
||||||
|
{
|
||||||
|
public uint value;
|
||||||
|
alias value this;
|
||||||
|
|
||||||
|
private ubyte[12] _padding;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* APIC registers.
|
||||||
|
*/
|
||||||
|
static struct ApicRegisters
|
||||||
|
{
|
||||||
|
ApicRegister[2] _reserved0;
|
||||||
|
ApicRegister lapic_id;
|
||||||
|
ApicRegister lapic_version;
|
||||||
|
ApicRegister[4] _reserved1;
|
||||||
|
ApicRegister task_priority;
|
||||||
|
ApicRegister arbitration_priority;
|
||||||
|
ApicRegister processor_priority;
|
||||||
|
ApicRegister eoi;
|
||||||
|
ApicRegister remote_read;
|
||||||
|
ApicRegister logical_destination;
|
||||||
|
ApicRegister destination_format;
|
||||||
|
ApicRegister spurious_interrupt_vector;
|
||||||
|
ApicRegister[8] in_service;
|
||||||
|
ApicRegister[8] trigger_mode;
|
||||||
|
ApicRegister[8] interrupt_request;
|
||||||
|
ApicRegister error_status;
|
||||||
|
ApicRegister[6] _reserved2;
|
||||||
|
ApicRegister lvt_cmci;
|
||||||
|
ApicRegister[2] interrupt_command;
|
||||||
|
ApicRegister lvt_timer;
|
||||||
|
ApicRegister lvt_thermal_sensor;
|
||||||
|
ApicRegister lvt_performance_monitoring_counters;
|
||||||
|
ApicRegister[2] lvt_lint;
|
||||||
|
ApicRegister lvt_error;
|
||||||
|
ApicRegister initial_count;
|
||||||
|
ApicRegister current_count;
|
||||||
|
ApicRegister[4] _reserved3;
|
||||||
|
ApicRegister divide_configuration;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* I/O APIC registers.
|
||||||
|
*/
|
||||||
|
static struct IoApicRegisters
|
||||||
|
{
|
||||||
|
ApicRegister address;
|
||||||
|
ApicRegister data;
|
||||||
|
}
|
||||||
|
|
||||||
|
alias ISRHandler = void function();
|
||||||
|
private static __gshared ApicRegisters * apic_registers;
|
||||||
|
private static __gshared IoApicRegisters * io_apic_registers;
|
||||||
|
private static __gshared ISRHandler[INT_APIC_COUNT] irq_handlers;
|
||||||
|
private static __gshared ubyte[INT_APIC_COUNT] irq_redirects;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize APIC.
|
||||||
|
*/
|
||||||
|
public static void initialize()
|
||||||
|
{
|
||||||
|
Klog.writefln("\a3Initializing APIC");
|
||||||
|
io_apic_registers = cast(IoApicRegisters *)DEFAULT_IO_APIC_ADDRESS;
|
||||||
|
for (ubyte i = 0u; i < INT_APIC_COUNT; i++)
|
||||||
|
{
|
||||||
|
irq_redirects[i] = i;
|
||||||
|
}
|
||||||
|
MADT * madt = cast(MADT *)Acpi.get_table("APIC");
|
||||||
|
madt.scan();
|
||||||
|
Klog.writefln("APIC found at %p", apic_registers);
|
||||||
|
Klog.writefln("I/O APIC found at %p", io_apic_registers);
|
||||||
|
Hurl.map(cast(ulong)apic_registers, cast(ulong)apic_registers,
|
||||||
|
PT_WRITABLE | PT_WRITE_THROUGH | PT_DISABLE_CACHE | PT_NO_EXECUTE);
|
||||||
|
Hurl.map(cast(ulong)io_apic_registers, cast(ulong)io_apic_registers,
|
||||||
|
PT_WRITABLE | PT_WRITE_THROUGH | PT_DISABLE_CACHE | PT_NO_EXECUTE);
|
||||||
|
/* Enable local APIC to receive interrupts and set spurious interrupt
|
||||||
|
* vector to 0xFF. */
|
||||||
|
apic_registers.spurious_interrupt_vector.value = 0x100u | INT_APIC_SPURIOUS;
|
||||||
|
apic_registers.lvt_timer.value = INT_APIC_TIMER;
|
||||||
|
apic_registers.lvt_lint[0].value = INT_APIC_LINT0;
|
||||||
|
apic_registers.lvt_lint[1].value = INT_APIC_LINT1;
|
||||||
|
/* Enable PIT interrupt. */
|
||||||
|
configure_io_apic_irq(IRQ_PIT, &Pit.isr);
|
||||||
|
/* Enable RTC interrupt. */
|
||||||
|
configure_io_apic_irq(IRQ_RTC, &Rtc.isr);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configure routing for an I/O APIC IRQ.
|
||||||
|
*/
|
||||||
|
private static void configure_io_apic_irq(size_t orig_irq, ISRHandler isr_handler)
|
||||||
|
{
|
||||||
|
size_t io_apic_irq = irq_redirects[orig_irq];
|
||||||
|
ulong entry = INT_APIC_BASE + io_apic_irq;
|
||||||
|
io_apic_registers.address.value = cast(uint)(0x10u + io_apic_irq * 2u);
|
||||||
|
io_apic_registers.data.value = entry & 0xFFFF_FFFFu;
|
||||||
|
io_apic_registers.address.value = cast(uint)(0x10u + io_apic_irq * 2u + 1u);
|
||||||
|
io_apic_registers.data.value = entry >> 32u;
|
||||||
|
irq_handlers[io_apic_irq] = isr_handler;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Signal APIC end of interrupt.
|
||||||
|
*/
|
||||||
|
public static void eoi()
|
||||||
|
{
|
||||||
|
apic_registers.eoi.value = 0u;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* APIC interrupt handler.
|
||||||
|
*/
|
||||||
|
public static void isr(ulong vector)
|
||||||
|
{
|
||||||
|
ulong io_apic_irq = vector - INT_APIC_BASE;
|
||||||
|
if ((io_apic_irq < INT_APIC_COUNT) && (irq_handlers[io_apic_irq] != null))
|
||||||
|
{
|
||||||
|
irq_handlers[io_apic_irq]();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Klog.writefln("Unhandled APIC ISR %u", vector);
|
||||||
|
}
|
||||||
|
eoi();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,77 @@
|
|||||||
|
/**
|
||||||
|
* HULK boot information.
|
||||||
|
*/
|
||||||
|
module hulk.bootinfo;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* HULK boot information structure.
|
||||||
|
*/
|
||||||
|
struct BootInfo
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Framebuffer description structure.
|
||||||
|
*/
|
||||||
|
struct Framebuffer
|
||||||
|
{
|
||||||
|
/** Framebuffer physical base address. */
|
||||||
|
uint * buffer;
|
||||||
|
/** Horizontal resolution. */
|
||||||
|
uint width;
|
||||||
|
/** Vertical resolution. */
|
||||||
|
uint height;
|
||||||
|
/** Number of pixels per scan line. */
|
||||||
|
uint stride;
|
||||||
|
/** Pixel format. */
|
||||||
|
uint format;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Memory map entry description structure.
|
||||||
|
*/
|
||||||
|
struct MemoryRegion
|
||||||
|
{
|
||||||
|
enum Type
|
||||||
|
{
|
||||||
|
Reserved,
|
||||||
|
Bootloader,
|
||||||
|
Conventional,
|
||||||
|
Unusable,
|
||||||
|
ACPIReclaim,
|
||||||
|
ACPINVS,
|
||||||
|
MemoryMappedIO,
|
||||||
|
MemoryMappedIOPortSpace,
|
||||||
|
PalCode,
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Base address of the memory region. */
|
||||||
|
ulong base;
|
||||||
|
/** Size in bytes of the memory region. */
|
||||||
|
size_t size;
|
||||||
|
/** Type of the memory region. */
|
||||||
|
ubyte type;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Framebuffer parameters. */
|
||||||
|
Framebuffer fb;
|
||||||
|
|
||||||
|
/* Memory map. */
|
||||||
|
MemoryRegion[1000] memory_map;
|
||||||
|
|
||||||
|
/* Number of memory map entries. */
|
||||||
|
size_t memory_map_count;
|
||||||
|
|
||||||
|
/* Physical address of HULK. */
|
||||||
|
ulong hulk_phys;
|
||||||
|
|
||||||
|
/* Physical address used for HULK bss section. */
|
||||||
|
ulong bss_phys;
|
||||||
|
|
||||||
|
/* Physical address of stack while jumping to HULK. */
|
||||||
|
ulong stack_phys;
|
||||||
|
|
||||||
|
/* Physical address of framebuffer buffer1. */
|
||||||
|
ulong fb_buffer1_phys;
|
||||||
|
|
||||||
|
/* Physical address of ACPI XSDT table. */
|
||||||
|
ulong acpi_xsdt_phys;
|
||||||
|
}
|
||||||
@@ -0,0 +1,276 @@
|
|||||||
|
/**
|
||||||
|
* HULK Console functionality.
|
||||||
|
*/
|
||||||
|
module hulk.console;
|
||||||
|
|
||||||
|
import hulk.fb;
|
||||||
|
import hulk.kfont;
|
||||||
|
import hulk.ver;
|
||||||
|
import hulk.rtc;
|
||||||
|
import hulk.writef;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represent the HULK console.
|
||||||
|
*
|
||||||
|
* Coordinate (0,0) is the top-left console position.
|
||||||
|
*/
|
||||||
|
struct Console
|
||||||
|
{
|
||||||
|
/** Border color. */
|
||||||
|
private enum BORDER_COLOR = 0xFF8000u;
|
||||||
|
|
||||||
|
/** Heading color. */
|
||||||
|
private enum HEADING_COLOR = 0x0066FFu;
|
||||||
|
|
||||||
|
/** Console page width in text columns. */
|
||||||
|
private static __gshared size_t m_width;
|
||||||
|
|
||||||
|
/** Console page height in text rows. */
|
||||||
|
private static __gshared size_t m_height;
|
||||||
|
|
||||||
|
/** Current console page. */
|
||||||
|
private static __gshared size_t m_page;
|
||||||
|
|
||||||
|
/** Current page cursor X position. */
|
||||||
|
private static __gshared size_t m_x;
|
||||||
|
|
||||||
|
/** Current page cursor Y position. */
|
||||||
|
private static __gshared size_t m_y;
|
||||||
|
|
||||||
|
/** Number of delayed newline characters. */
|
||||||
|
private static __gshared size_t m_newlines;
|
||||||
|
|
||||||
|
/** Active console escape code. */
|
||||||
|
private static __gshared char m_escape_code;
|
||||||
|
|
||||||
|
/** Flag to indicate the previous character was an escape character. */
|
||||||
|
private static __gshared bool m_escape;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize the console.
|
||||||
|
*/
|
||||||
|
public static void initialize()
|
||||||
|
{
|
||||||
|
size_t fb_console_height = Fb.height - Kfont.line_height - 1;
|
||||||
|
m_width = (Fb.width - 1) / 2 / Kfont.advance;
|
||||||
|
m_height = fb_console_height / Kfont.line_height;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clear the console.
|
||||||
|
*/
|
||||||
|
public static void clear()
|
||||||
|
{
|
||||||
|
Fb.clear();
|
||||||
|
m_page = 0u;
|
||||||
|
m_x = 0u;
|
||||||
|
m_y = 0u;
|
||||||
|
draw();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write a character to the console (buffer newlines).
|
||||||
|
*
|
||||||
|
* @param ch Character to write.
|
||||||
|
*/
|
||||||
|
public static void write(char ch)
|
||||||
|
{
|
||||||
|
if (ch == '\n')
|
||||||
|
{
|
||||||
|
render_heading_end();
|
||||||
|
m_escape_code = 0u;
|
||||||
|
m_newlines++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
dowrite(ch);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void render_heading_start()
|
||||||
|
{
|
||||||
|
if ('0' <= m_escape_code && m_escape_code <= '9' && m_x < (m_width - 1))
|
||||||
|
{
|
||||||
|
size_t heading_level = m_escape_code - ('0' - 1);
|
||||||
|
size_t nx = m_x + heading_level;
|
||||||
|
if (nx >= m_width)
|
||||||
|
{
|
||||||
|
nx = m_width - 1;
|
||||||
|
}
|
||||||
|
size_t hx = fb_x(m_page, m_x);
|
||||||
|
size_t hy = fb_y(m_y) + Kfont.line_height / 2 - 1;
|
||||||
|
size_t hwidth = (nx - m_x - 1) * Kfont.advance + Kfont.advance / 2 - 1;
|
||||||
|
Fb.rect(hx, hy, hwidth, 2, HEADING_COLOR);
|
||||||
|
Fb.rect(hx + hwidth, fb_y(m_y) + Kfont.line_height / 8,
|
||||||
|
2, Kfont.line_height * 3 / 4, HEADING_COLOR);
|
||||||
|
m_x = nx;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void render_heading_end()
|
||||||
|
{
|
||||||
|
if ('0' <= m_escape_code && m_escape_code <= '9' && m_x < (m_width - 1))
|
||||||
|
{
|
||||||
|
size_t heading_level = m_escape_code - ('0' - 1);
|
||||||
|
size_t nx = m_x + heading_level;
|
||||||
|
if (nx >= m_width)
|
||||||
|
{
|
||||||
|
nx = m_width - 1;
|
||||||
|
}
|
||||||
|
size_t hwidth = (nx - m_x - 1) * Kfont.advance + Kfont.advance / 2 - 1;
|
||||||
|
size_t hx = fb_x(m_page, nx) - hwidth;
|
||||||
|
size_t hy = fb_y(m_y) + Kfont.line_height / 2 - 1;
|
||||||
|
Fb.rect(hx, hy, hwidth, 2, HEADING_COLOR);
|
||||||
|
Fb.rect(hx - 2, fb_y(m_y) + Kfont.line_height / 8,
|
||||||
|
2, Kfont.line_height * 3 / 4, HEADING_COLOR);
|
||||||
|
m_x = nx;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write a character to the console (no newline buffering).
|
||||||
|
*
|
||||||
|
* @param ch Character to write.
|
||||||
|
*/
|
||||||
|
private static void dowrite(char ch)
|
||||||
|
{
|
||||||
|
if (m_newlines != 0u)
|
||||||
|
{
|
||||||
|
size_t newlines = m_newlines;
|
||||||
|
m_newlines = 0u;
|
||||||
|
for (size_t i = 0u; i < newlines; i++)
|
||||||
|
{
|
||||||
|
dowrite('\n');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (m_escape)
|
||||||
|
{
|
||||||
|
m_escape_code = ch;
|
||||||
|
render_heading_start();
|
||||||
|
m_escape = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (ch == '\a')
|
||||||
|
{
|
||||||
|
m_escape = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (ch == '\n')
|
||||||
|
{
|
||||||
|
m_x = 0u;
|
||||||
|
m_y++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
render_char(fb_x(m_page, m_x), fb_y(m_y), ch);
|
||||||
|
m_x++;
|
||||||
|
if (m_x == m_width)
|
||||||
|
{
|
||||||
|
m_x = 0u;
|
||||||
|
m_y++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (m_y == m_height)
|
||||||
|
{
|
||||||
|
if (m_page == 0u)
|
||||||
|
{
|
||||||
|
m_page = 1u;
|
||||||
|
m_x = 0u;
|
||||||
|
m_y = 0u;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
m_y--;
|
||||||
|
shift_rows();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shift console rows.
|
||||||
|
*/
|
||||||
|
private static void shift_rows()
|
||||||
|
{
|
||||||
|
/* Shift up page 0 */
|
||||||
|
Fb.copy_rect(fb_x(0u, 0u), fb_y(1u),
|
||||||
|
m_width * Kfont.advance, (m_height - 1u) * Kfont.line_height,
|
||||||
|
fb_x(0u, 0u), fb_y(0u));
|
||||||
|
/* Copy page 1 first row to page 0 last row. */
|
||||||
|
Fb.copy_rect(fb_x(1u, 0u), fb_y(0u),
|
||||||
|
m_width * Kfont.advance, Kfont.line_height,
|
||||||
|
fb_x(0u, 0u), fb_y(m_height - 1u));
|
||||||
|
/* Shift up page 1 */
|
||||||
|
Fb.copy_rect(fb_x(1u, 0u), fb_y(1u),
|
||||||
|
m_width * Kfont.advance, (m_height - 1u) * Kfont.line_height,
|
||||||
|
fb_x(1u, 0u), fb_y(0u));
|
||||||
|
/* Erase page 1 last row. */
|
||||||
|
Fb.rect(fb_x(1u, 0u), fb_y(m_height - 1u),
|
||||||
|
m_width * Kfont.advance, Kfont.line_height, 0u);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Render a character.
|
||||||
|
*
|
||||||
|
* @param x X position.
|
||||||
|
* @param y Y position.
|
||||||
|
* @param ch Character to render.
|
||||||
|
*/
|
||||||
|
private static void render_char(size_t x, size_t y, char ch)
|
||||||
|
{
|
||||||
|
if (ch > 127)
|
||||||
|
{
|
||||||
|
ch = 0u;
|
||||||
|
}
|
||||||
|
const(CharInfo) * ci = &Kfont.chars[ch];
|
||||||
|
Fb.blit_alpha_bitmap(x + ci.left, y + ci.top, ci.bitmap, ci.width, ci.height);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the framebuffer X coordinate corresponding to the console page X position.
|
||||||
|
*/
|
||||||
|
private static size_t fb_x(size_t page, size_t x)
|
||||||
|
{
|
||||||
|
return x * Kfont.advance + page * (m_width * Kfont.advance + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the framebuffer Y coordinate corresponding to the console page Y position.
|
||||||
|
*/
|
||||||
|
private static size_t fb_y(size_t y)
|
||||||
|
{
|
||||||
|
return (y + 1) * Kfont.line_height + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Draw console.
|
||||||
|
*/
|
||||||
|
private static draw()
|
||||||
|
{
|
||||||
|
static __gshared string header_text = "Welcome to HOS v" ~ VERSION ~ "!";
|
||||||
|
Fb.rect(0u, Kfont.line_height, Fb.width, 1, BORDER_COLOR);
|
||||||
|
Fb.rect(m_width * Kfont.advance, Kfont.line_height, 1, Fb.height - Kfont.line_height, BORDER_COLOR);
|
||||||
|
size_t x = 0;
|
||||||
|
foreach (c; header_text)
|
||||||
|
{
|
||||||
|
render_char(x, 0, c);
|
||||||
|
x += Kfont.advance;
|
||||||
|
}
|
||||||
|
update_header();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update console header.
|
||||||
|
*/
|
||||||
|
public static update_header()
|
||||||
|
{
|
||||||
|
__gshared uint x;
|
||||||
|
x = Fb.width - 8 * Kfont.advance;
|
||||||
|
Fb.rect(x - 1, 0, 1, Kfont.line_height, BORDER_COLOR);
|
||||||
|
Fb.rect(x, 0, Fb.width - x, Kfont.line_height, 0);
|
||||||
|
Rtc.time rtc_time = Rtc.read_rtc_time();
|
||||||
|
writef(function(ubyte ch) {
|
||||||
|
render_char(x, 0, ch);
|
||||||
|
x += Kfont.advance;
|
||||||
|
}, "%02u:%02u:%02u", rtc_time.hour, rtc_time.minute, rtc_time.second);
|
||||||
|
}
|
||||||
|
}
|
||||||
+261
@@ -0,0 +1,261 @@
|
|||||||
|
/**
|
||||||
|
* CPU functionality.
|
||||||
|
*/
|
||||||
|
module hulk.cpu;
|
||||||
|
|
||||||
|
import ldc.llvmasm;
|
||||||
|
|
||||||
|
/** CR0 bits. @{ */
|
||||||
|
enum ulong CR0_PE = 0x1u;
|
||||||
|
enum ulong CR0_MP = 0x2u;
|
||||||
|
enum ulong CR0_EM = 0x4u;
|
||||||
|
enum ulong CR0_TS = 0x8u;
|
||||||
|
enum ulong CR0_ET = 0x10u;
|
||||||
|
enum ulong CR0_NE = 0x20u;
|
||||||
|
enum ulong CR0_WP = 0x1_0000u;
|
||||||
|
enum ulong CR0_AM = 0x4_0000u;
|
||||||
|
enum ulong CR0_NW = 0x2000_0000u;
|
||||||
|
enum ulong CR0_CD = 0x4000_0000u;
|
||||||
|
enum ulong CR0_PG = 0x8000_0000u;
|
||||||
|
/** @} */
|
||||||
|
|
||||||
|
/** CR4 bits. @{ */
|
||||||
|
enum ulong CR4_VME = 0x1u;
|
||||||
|
enum ulong CR4_PVI = 0x2u;
|
||||||
|
enum ulong CR4_TSD = 0x4u;
|
||||||
|
enum ulong CR4_DE = 0x8u;
|
||||||
|
enum ulong CR4_PSE = 0x10u;
|
||||||
|
enum ulong CR4_PAE = 0x20u;
|
||||||
|
enum ulong CR4_MCE = 0x40u;
|
||||||
|
enum ulong CR4_PGE = 0x80u;
|
||||||
|
enum ulong CR4_PCE = 0x100u;
|
||||||
|
enum ulong CR4_OSFXSR = 0x200u;
|
||||||
|
enum ulong CR4_OSXMMEXCPT = 0x400u;
|
||||||
|
enum ulong CR4_UMIP = 0x800u;
|
||||||
|
enum ulong CR4_VMXE = 0x2000u;
|
||||||
|
enum ulong CR4_SMXE = 0x4000u;
|
||||||
|
enum ulong CR4_FSGSBASE = 0x1_0000u;
|
||||||
|
enum ulong CR4_PCIDE = 0x2_0000u;
|
||||||
|
enum ulong CR4_OSXSAVE = 0x4_0000u;
|
||||||
|
enum ulong CR4_SMEP = 0x10_0000u;
|
||||||
|
enum ulong CR4_SMAP = 0x20_0000u;
|
||||||
|
enum ulong CR4_PKE = 0x40_0000u;
|
||||||
|
enum ulong CR4_CET = 0x80_0000u;
|
||||||
|
enum ulong CR4_PKS = 0x100_0000u;
|
||||||
|
/** @} */
|
||||||
|
|
||||||
|
/** XCR0 bits. @{ */
|
||||||
|
enum ulong XCR0_X87 = 0x1u;
|
||||||
|
enum ulong XCR0_SSE = 0x2u;
|
||||||
|
enum ulong XCR0_AVX = 0x4u;
|
||||||
|
enum ulong XCR0_BNDREG = 0x8u;
|
||||||
|
enum ulong XCR0_BNDSCR = 0x10u;
|
||||||
|
enum ulong XCR0_OPMASK = 0x20u;
|
||||||
|
enum ulong XCR0_ZMM_HI256 = 0x40u;
|
||||||
|
enum ulong XCR0_HI16_ZMM = 0x80u;
|
||||||
|
enum ulong XCR0_PKRU = 0x100u;
|
||||||
|
/** @} */
|
||||||
|
|
||||||
|
/** MSR register numbers. @{ */
|
||||||
|
enum uint MSR_PAT = 0x277u;
|
||||||
|
enum uint MSR_EFER = 0xC000_0080u;
|
||||||
|
/** @} */
|
||||||
|
|
||||||
|
/** EFER flags. @{ */
|
||||||
|
enum uint EFER_SCE = 0x1u;
|
||||||
|
enum uint EFER_LME = 0x100u;
|
||||||
|
enum uint EFER_LMA = 0x400u;
|
||||||
|
enum uint EFER_NXE = 0x800u;
|
||||||
|
enum uint EFER_SVME = 0x1000u;
|
||||||
|
enum uint EFER_LMSLE = 0x2000u;
|
||||||
|
enum uint EFER_FFXSR = 0x4000u;
|
||||||
|
enum uint EFER_TCE = 0x8000u;
|
||||||
|
/** @} */
|
||||||
|
|
||||||
|
/** CPUID 1 bits. @{ */
|
||||||
|
enum uint CPUID_1_EDX_FPU = 0x1u;
|
||||||
|
enum uint CPUID_1_EDX_VME = 0x2u;
|
||||||
|
enum uint CPUID_1_EDX_DE = 0x4u;
|
||||||
|
enum uint CPUID_1_EDX_PSE = 0x8u;
|
||||||
|
enum uint CPUID_1_EDX_TSC = 0x10u;
|
||||||
|
enum uint CPUID_1_EDX_MSR = 0x20u;
|
||||||
|
enum uint CPUID_1_EDX_PAE = 0x40u;
|
||||||
|
enum uint CPUID_1_EDX_MCE = 0x80u;
|
||||||
|
enum uint CPUID_1_EDX_CX8 = 0x100u;
|
||||||
|
enum uint CPUID_1_EDX_APIC = 0x200u;
|
||||||
|
enum uint CPUID_1_EDX_SEP = 0x800u;
|
||||||
|
enum uint CPUID_1_EDX_MTRR = 0x1000u;
|
||||||
|
enum uint CPUID_1_EDX_PGE = 0x2000u;
|
||||||
|
enum uint CPUID_1_EDX_MCA = 0x4000u;
|
||||||
|
enum uint CPUID_1_EDX_CMOV = 0x8000u;
|
||||||
|
enum uint CPUID_1_EDX_PAT = 0x1_0000u;
|
||||||
|
enum uint CPUID_1_EDX_PSE36 = 0x2_0000u;
|
||||||
|
enum uint CPUID_1_EDX_PSN = 0x4_0000u;
|
||||||
|
enum uint CPUID_1_EDX_CLFSH = 0x8_0000u;
|
||||||
|
enum uint CPUID_1_EDX_DS = 0x20_0000u;
|
||||||
|
enum uint CPUID_1_EDX_ACPI = 0x40_0000u;
|
||||||
|
enum uint CPUID_1_EDX_MMX = 0x80_0000u;
|
||||||
|
enum uint CPUID_1_EDX_FXSR = 0x100_0000u;
|
||||||
|
enum uint CPUID_1_EDX_SSE = 0x200_0000u;
|
||||||
|
enum uint CPUID_1_EDX_SSE2 = 0x400_0000u;
|
||||||
|
enum uint CPUID_1_EDX_SS = 0x800_0000u;
|
||||||
|
enum uint CPUID_1_EDX_HTT = 0x1000_0000u;
|
||||||
|
enum uint CPUID_1_EDX_TM = 0x2000_0000u;
|
||||||
|
enum uint CPUID_1_EDX_IA64 = 0x4000_0000u;
|
||||||
|
enum uint CPUID_1_EDX_PBE = 0x8000_0000u;
|
||||||
|
enum uint CPUID_1_ECX_SSE3 = 0x1u;
|
||||||
|
enum uint CPUID_1_ECX_PCLMULQDQ = 0x2u;
|
||||||
|
enum uint CPUID_1_ECX_DTES64 = 0x4u;
|
||||||
|
enum uint CPUID_1_ECX_MONITOR = 0x8u;
|
||||||
|
enum uint CPUID_1_ECX_DSCPL = 0x10u;
|
||||||
|
enum uint CPUID_1_ECX_VMX = 0x20u;
|
||||||
|
enum uint CPUID_1_ECX_SMX = 0x40u;
|
||||||
|
enum uint CPUID_1_ECX_EST = 0x80u;
|
||||||
|
enum uint CPUID_1_ECX_TM2 = 0x100u;
|
||||||
|
enum uint CPUID_1_ECX_SSSE3 = 0x200u;
|
||||||
|
enum uint CPUID_1_ECX_CNXTID = 0x400u;
|
||||||
|
enum uint CPUID_1_ECX_SDBG = 0x800u;
|
||||||
|
enum uint CPUID_1_ECX_FMA = 0x1000u;
|
||||||
|
enum uint CPUID_1_ECX_CX16 = 0x2000u;
|
||||||
|
enum uint CPUID_1_ECX_XTPR = 0x4000u;
|
||||||
|
enum uint CPUID_1_ECX_PDCM = 0x8000u;
|
||||||
|
enum uint CPUID_1_ECX_PCID = 0x2_0000u;
|
||||||
|
enum uint CPUID_1_ECX_DCA = 0x4_0000u;
|
||||||
|
enum uint CPUID_1_ECX_SSE41 = 0x8_0000u;
|
||||||
|
enum uint CPUID_1_ECX_SSE42 = 0x10_0000u;
|
||||||
|
enum uint CPUID_1_ECX_X2APIC = 0x20_0000u;
|
||||||
|
enum uint CPUID_1_ECX_MOVBE = 0x40_0000u;
|
||||||
|
enum uint CPUID_1_ECX_POPCNT = 0x80_0000u;
|
||||||
|
enum uint CPUID_1_ECX_TSCDEADLINE = 0x100_0000u;
|
||||||
|
enum uint CPUID_1_ECX_AES = 0x200_0000u;
|
||||||
|
enum uint CPUID_1_ECX_XSAVE = 0x400_0000u;
|
||||||
|
enum uint CPUID_1_ECX_OSXSAVE = 0x800_0000u;
|
||||||
|
enum uint CPUID_1_ECX_AVX = 0x1000_0000u;
|
||||||
|
enum uint CPUID_1_ECX_F16C = 0x2000_0000u;
|
||||||
|
enum uint CPUID_1_ECX_RDRND = 0x4000_0000u;
|
||||||
|
enum uint CPUID_1_ECX_HYPERVISOR = 0x8000_0000u;
|
||||||
|
/** @} */
|
||||||
|
|
||||||
|
void cli()
|
||||||
|
{
|
||||||
|
__asm("cli", "");
|
||||||
|
}
|
||||||
|
|
||||||
|
void sti()
|
||||||
|
{
|
||||||
|
__asm("sti", "");
|
||||||
|
}
|
||||||
|
|
||||||
|
void hlt()
|
||||||
|
{
|
||||||
|
__asm("hlt", "");
|
||||||
|
}
|
||||||
|
|
||||||
|
ulong read_cr0()
|
||||||
|
{
|
||||||
|
return __asm!ulong("mov %cr0, %rax", "={rax}");
|
||||||
|
}
|
||||||
|
|
||||||
|
void write_cr0(ulong v)
|
||||||
|
{
|
||||||
|
__asm("mov $0, %cr0", "r", v);
|
||||||
|
}
|
||||||
|
|
||||||
|
ulong read_cr2()
|
||||||
|
{
|
||||||
|
return __asm!ulong("mov %cr2, %rax", "={rax}");
|
||||||
|
}
|
||||||
|
|
||||||
|
ulong read_cr3()
|
||||||
|
{
|
||||||
|
return __asm!ulong("mov %cr3, %rax", "={rax}");
|
||||||
|
}
|
||||||
|
|
||||||
|
void write_cr3(ulong v)
|
||||||
|
{
|
||||||
|
__asm("mov $0, %cr3", "r", v);
|
||||||
|
}
|
||||||
|
|
||||||
|
ulong read_cr4()
|
||||||
|
{
|
||||||
|
return __asm!ulong("mov %cr4, %rax", "={rax}");
|
||||||
|
}
|
||||||
|
|
||||||
|
void write_cr4(ulong v)
|
||||||
|
{
|
||||||
|
__asm("mov $0, %cr4", "r", v);
|
||||||
|
}
|
||||||
|
|
||||||
|
ulong read_rflags()
|
||||||
|
{
|
||||||
|
return __asm!ulong(`
|
||||||
|
pushf
|
||||||
|
mov (%rsp), %rax
|
||||||
|
add $$8, %rsp`, "={rax}");
|
||||||
|
}
|
||||||
|
|
||||||
|
ulong rdmsr(uint msr)
|
||||||
|
{
|
||||||
|
return __asm!ulong(`
|
||||||
|
rdmsr
|
||||||
|
shl $$32, %rdx
|
||||||
|
or %rdx, %rax`, "={rax},{ecx},~{rdx}", msr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void wrmsr(uint msr, ulong value)
|
||||||
|
{
|
||||||
|
__asm(`wrmsr`, "{ecx},{edx},{eax}", msr, value >> 32u, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
ulong xgetbv(uint xcr)
|
||||||
|
{
|
||||||
|
return __asm!ulong(`
|
||||||
|
xgetbv
|
||||||
|
shl $$32, %rdx
|
||||||
|
or %rdx, %rax`, "={rax},{ecx},~{rdx}", xcr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void xsetbv(uint xcr, ulong value)
|
||||||
|
{
|
||||||
|
__asm(`xsetbv`, "{ecx},{edx},{eax}", xcr, value >> 32u, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
ubyte in8(ushort ioaddr)
|
||||||
|
{
|
||||||
|
return __asm!ubyte("inb %dx, %al", "={al},{dx}", ioaddr);
|
||||||
|
}
|
||||||
|
|
||||||
|
ushort in16(ushort ioaddr)
|
||||||
|
{
|
||||||
|
return __asm!ushort("inw %dx, %ax", "={ax},{dx}", ioaddr);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint in32(ushort ioaddr)
|
||||||
|
{
|
||||||
|
return __asm!uint("inl %dx, %eax", "={eax},{dx}", ioaddr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void out8(ushort ioaddr, ubyte v)
|
||||||
|
{
|
||||||
|
__asm("outb %al, %dx", "{dx},{al}", ioaddr, v);
|
||||||
|
}
|
||||||
|
|
||||||
|
void out16(ushort ioaddr, ushort v)
|
||||||
|
{
|
||||||
|
__asm("outw %ax, %dx", "{dx},{ax}", ioaddr, v);
|
||||||
|
}
|
||||||
|
|
||||||
|
void out32(ushort ioaddr, uint v)
|
||||||
|
{
|
||||||
|
__asm("outl %eax, %dx", "{dx},{eax}", ioaddr, v);
|
||||||
|
}
|
||||||
|
|
||||||
|
void cpuid1(uint * ebx, uint * ecx, uint * edx)
|
||||||
|
{
|
||||||
|
__asm("cpuid", "=*{ebx},=*{ecx},=*{edx},{eax}", ebx, ecx, edx, 1u);
|
||||||
|
}
|
||||||
|
|
||||||
|
void swint(ubyte swint_id)()
|
||||||
|
{
|
||||||
|
mixin(`__asm("int $$`, cast(int)cast(uint)swint_id, `", "");`);
|
||||||
|
}
|
||||||
+242
@@ -0,0 +1,242 @@
|
|||||||
|
/**
|
||||||
|
* HULK Framebuffer support.
|
||||||
|
*/
|
||||||
|
module hulk.fb;
|
||||||
|
|
||||||
|
import hulk.memory;
|
||||||
|
import hulk.kfont;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represent a graphical frame buffer.
|
||||||
|
*
|
||||||
|
* Coordinate (0,0) is the top-left corner of the framebuffer.
|
||||||
|
*
|
||||||
|
* TODO: Handle other pixel formats. Currently only BGRx is supported.
|
||||||
|
*/
|
||||||
|
struct Fb
|
||||||
|
{
|
||||||
|
/** Device frame buffer base address. */
|
||||||
|
private __gshared static uint * m_device_buffer;
|
||||||
|
|
||||||
|
/** Frame buffer 1 base address. */
|
||||||
|
private __gshared static uint * m_buffer1;
|
||||||
|
|
||||||
|
/** Frame buffer width. */
|
||||||
|
private __gshared static uint m_width;
|
||||||
|
|
||||||
|
/** Frame buffer height. */
|
||||||
|
private __gshared static uint m_height;
|
||||||
|
|
||||||
|
/** Frame buffer stride. */
|
||||||
|
private __gshared static uint m_stride;
|
||||||
|
|
||||||
|
/** Number of pixels in the frame buffer (whether visible or not). */
|
||||||
|
private __gshared static uint m_buffer_size;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the framebuffer width.
|
||||||
|
*/
|
||||||
|
public static @property uint width()
|
||||||
|
{
|
||||||
|
return m_width;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the framebuffer height.
|
||||||
|
*/
|
||||||
|
public static @property uint height()
|
||||||
|
{
|
||||||
|
return m_height;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize a frame buffer.
|
||||||
|
*
|
||||||
|
* @param device_buffer Device frame buffer base address.
|
||||||
|
* @param buffer1 Frame buffer 1 base address.
|
||||||
|
* @param width Frame buffer width.
|
||||||
|
* @param height Frame buffer height.
|
||||||
|
* @param stride Frame buffer stride.
|
||||||
|
*/
|
||||||
|
static void initialize(uint * device_buffer, uint * buffer1, uint width, uint height, uint stride)
|
||||||
|
{
|
||||||
|
m_device_buffer = device_buffer;
|
||||||
|
m_buffer1 = buffer1;
|
||||||
|
m_width = width;
|
||||||
|
m_height = height;
|
||||||
|
m_stride = stride;
|
||||||
|
m_buffer_size = stride * height;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clear the frame buffer to the given color.
|
||||||
|
*
|
||||||
|
* @param color Color to clear to.
|
||||||
|
*/
|
||||||
|
static void clear(uint color = 0u)
|
||||||
|
{
|
||||||
|
memset32(m_buffer1, color, m_buffer_size);
|
||||||
|
memset32(m_device_buffer, color, m_buffer_size);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Draw a solid rectangle on the framebuffer.
|
||||||
|
*
|
||||||
|
* @param x X coordinate of left of rectangle.
|
||||||
|
* @param y Y coordinate of top of rectangle.
|
||||||
|
* @param width Width of rectangle.
|
||||||
|
* @param height Height of rectangle.
|
||||||
|
* @param color Color of rectangle.
|
||||||
|
*/
|
||||||
|
static void rect(size_t x, size_t y, size_t width, size_t height, uint color)
|
||||||
|
{
|
||||||
|
for (size_t iy = 0u; iy < height; iy++)
|
||||||
|
{
|
||||||
|
size_t buffer_index = buffer_index(x, y);
|
||||||
|
memset32(&m_buffer1[buffer_index], color, width);
|
||||||
|
memset32(&m_device_buffer[buffer_index], color, width);
|
||||||
|
y++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Draw a horizontal line.
|
||||||
|
*
|
||||||
|
* @param x X coordinate of left of line.
|
||||||
|
* @param y Y coordinate of line.
|
||||||
|
* @param width Width of line.
|
||||||
|
* @param color Color of line.
|
||||||
|
*/
|
||||||
|
static void hline(size_t x, size_t y, size_t width, uint color)
|
||||||
|
{
|
||||||
|
size_t buffer_index = buffer_index(x, y);
|
||||||
|
memset32(&m_buffer1[buffer_index], color, width);
|
||||||
|
memset32(&m_device_buffer[buffer_index], color, width);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Draw a vertical line.
|
||||||
|
*
|
||||||
|
* @param x X coordinate of line.
|
||||||
|
* @param y Y coordinate of top of line.
|
||||||
|
* @param height Height of line.
|
||||||
|
* @param color Color of line.
|
||||||
|
*/
|
||||||
|
static void vline(size_t x, size_t y, size_t height, uint color)
|
||||||
|
{
|
||||||
|
size_t buffer_index = buffer_index(x, y);
|
||||||
|
for (size_t iy = 0u; iy < height; iy++)
|
||||||
|
{
|
||||||
|
m_buffer1[buffer_index] = color;
|
||||||
|
m_device_buffer[buffer_index] = color;
|
||||||
|
buffer_index += m_stride;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Blit an 8-bit alpha bitmap to the framebuffer.
|
||||||
|
* The foreground will be white (based on the alpha value), and the
|
||||||
|
* background black.
|
||||||
|
*
|
||||||
|
* @param x X coordinate of left of target location.
|
||||||
|
* @param y Y coordinate of top of target location.
|
||||||
|
* @param alpha_bitmap 8-bit alpha-channel bitmap.
|
||||||
|
* @param width Bitmap width.
|
||||||
|
* @param height Bitmap height.
|
||||||
|
*/
|
||||||
|
static void blit_alpha_bitmap(size_t x, size_t y, const(ubyte) * alpha_bitmap,
|
||||||
|
size_t width, size_t height)
|
||||||
|
{
|
||||||
|
size_t bitmap_index;
|
||||||
|
for (size_t iy = 0u; iy < height; iy++)
|
||||||
|
{
|
||||||
|
size_t row_buffer_index = buffer_index(x, y);
|
||||||
|
for (size_t ix = 0u; ix < width; ix++)
|
||||||
|
{
|
||||||
|
size_t buffer_index = row_buffer_index + ix;
|
||||||
|
ubyte alpha = alpha_bitmap[bitmap_index];
|
||||||
|
m_buffer1[buffer_index] = (alpha << 16u) | (alpha << 8u) | alpha;
|
||||||
|
bitmap_index++;
|
||||||
|
}
|
||||||
|
memcpy32(&m_device_buffer[row_buffer_index], &m_buffer1[row_buffer_index], width);
|
||||||
|
y++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Blit an 8-bit alpha bitmap to the framebuffer.
|
||||||
|
* The foreground will be white (based on the alpha value), and the
|
||||||
|
* background black.
|
||||||
|
*
|
||||||
|
* @param x X coordinate of left of target location.
|
||||||
|
* @param y Y coordinate of top of target location.
|
||||||
|
* @param alpha_bitmap 8-bit alpha-channel bitmap.
|
||||||
|
* @param width Bitmap width.
|
||||||
|
* @param height Bitmap height.
|
||||||
|
*/
|
||||||
|
static void blit_alpha_bitmap(size_t x, size_t y, const(ubyte)[] alpha_bitmap,
|
||||||
|
size_t width, size_t height)
|
||||||
|
{
|
||||||
|
blit_alpha_bitmap(x, y, alpha_bitmap.ptr, width, height);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copy framebuffer rows up in the framebuffer.
|
||||||
|
*
|
||||||
|
* @param y Y coordinate of top of region to copy up.
|
||||||
|
* @param height Height of region to copy up.
|
||||||
|
* @param offset Number of rows to copy region up by.
|
||||||
|
*/
|
||||||
|
static void copy_rows_up(size_t y, size_t height, size_t offset)
|
||||||
|
{
|
||||||
|
size_t dest_buffer_index = buffer_index(0u, y - offset);
|
||||||
|
size_t src_buffer_index = buffer_index(0u, y);
|
||||||
|
memcpy32(&m_buffer1[dest_buffer_index],
|
||||||
|
&m_buffer1[src_buffer_index],
|
||||||
|
(m_stride * height));
|
||||||
|
memcpy32(&m_device_buffer[dest_buffer_index],
|
||||||
|
&m_buffer1[dest_buffer_index],
|
||||||
|
(m_stride * height));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copy a rectangle in the framebuffer.
|
||||||
|
*
|
||||||
|
* @param sx X coordinate of left of source rectangle.
|
||||||
|
* @param sy Y coordinate of top of source rectangle.
|
||||||
|
* @param width Width of rectangle to copy.
|
||||||
|
* @param height Height of rectangle to copy.
|
||||||
|
* @param dx X coordinate of left of destination rectangle.
|
||||||
|
* @param dy Y coordinate of top of destination rectangle.
|
||||||
|
*/
|
||||||
|
static void copy_rect(size_t sx, size_t sy, size_t width, size_t height,
|
||||||
|
size_t dx, size_t dy)
|
||||||
|
{
|
||||||
|
size_t dest_buffer_index = buffer_index(dx, dy);
|
||||||
|
size_t src_buffer_index = buffer_index(sx, sy);
|
||||||
|
for (size_t y = 0u; y < height; y++)
|
||||||
|
{
|
||||||
|
memcpy32(&m_buffer1[dest_buffer_index],
|
||||||
|
&m_buffer1[src_buffer_index],
|
||||||
|
width);
|
||||||
|
memcpy32(&m_device_buffer[dest_buffer_index],
|
||||||
|
&m_buffer1[dest_buffer_index],
|
||||||
|
width);
|
||||||
|
dest_buffer_index += m_stride;
|
||||||
|
src_buffer_index += m_stride;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the buffer index for the given X and Y coordinates.
|
||||||
|
*
|
||||||
|
* @param x X coordinate from left side of framebuffer.
|
||||||
|
* @param y Y coordinate from bottom side of framebuffer.
|
||||||
|
*
|
||||||
|
* @return Buffer index for the given X and Y coordinates.
|
||||||
|
*/
|
||||||
|
private static size_t buffer_index(size_t x, size_t y)
|
||||||
|
{
|
||||||
|
return y * m_stride + x;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
/**
|
||||||
|
* GDT (Global Descriptor Table) functionality.
|
||||||
|
*/
|
||||||
|
module hulk.gdt;
|
||||||
|
|
||||||
|
import ldc.llvmasm;
|
||||||
|
|
||||||
|
struct Gdt
|
||||||
|
{
|
||||||
|
struct gdtr_t
|
||||||
|
{
|
||||||
|
ushort limit;
|
||||||
|
align(2) ulong offset;
|
||||||
|
}
|
||||||
|
static assert(gdtr_t.sizeof == 10u);
|
||||||
|
|
||||||
|
public enum size_t SELECTOR_NULL = 0x00u;
|
||||||
|
public enum size_t SELECTOR_KERNEL_CODE = 0x08u;
|
||||||
|
public enum size_t SELECTOR_KERNEL_DATA = 0x10u;
|
||||||
|
public enum size_t SELECTOR_COUNT = 3u;
|
||||||
|
|
||||||
|
static __gshared ulong[SELECTOR_COUNT] gdt;
|
||||||
|
static __gshared gdtr_t gdtr;
|
||||||
|
|
||||||
|
public static void initialize()
|
||||||
|
{
|
||||||
|
gdt[SELECTOR_KERNEL_CODE / ulong.sizeof] = 0x00_A_F_9A_000000_0000u;
|
||||||
|
gdt[SELECTOR_KERNEL_DATA / ulong.sizeof] = 0x00_C_F_92_000000_0000u;
|
||||||
|
gdtr.limit = gdt.sizeof - 1u;
|
||||||
|
gdtr.offset = cast(ulong)&gdt;
|
||||||
|
lgdt(&gdtr);
|
||||||
|
load_data_selectors(SELECTOR_KERNEL_DATA);
|
||||||
|
load_code_selector(SELECTOR_KERNEL_CODE);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void lgdt(gdtr_t * gdtr)
|
||||||
|
{
|
||||||
|
__asm("lgdt $0", "*m", gdtr);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void load_data_selectors(ulong selector)
|
||||||
|
{
|
||||||
|
__asm(`mov %ax, %ds
|
||||||
|
mov %ax, %es
|
||||||
|
mov %ax, %fs
|
||||||
|
mov %ax, %gs
|
||||||
|
mov %ax, %ss`, "{rax}", selector);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void load_code_selector(ulong selector)
|
||||||
|
{
|
||||||
|
__asm(`push %rax
|
||||||
|
movabs $$1f, %rax
|
||||||
|
push %rax
|
||||||
|
lretq
|
||||||
|
1:`, "{rax}", selector);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
/**
|
||||||
|
* HULK image header.
|
||||||
|
*/
|
||||||
|
module hulk.header;
|
||||||
|
|
||||||
|
import hulk.bootinfo;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Header describing the HULK image.
|
||||||
|
*/
|
||||||
|
struct HulkHeader
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* HULK BSS size.
|
||||||
|
*
|
||||||
|
* The OS loader must map memory to a zeroed range of this size.
|
||||||
|
*
|
||||||
|
* This value is really an integer, but is stored as a pointer because it
|
||||||
|
* is provided by the linker and LDC does not like us to cast it.
|
||||||
|
*/
|
||||||
|
void * hulk_bss_size;
|
||||||
|
|
||||||
|
/** Entry point. */
|
||||||
|
void * entry;
|
||||||
|
|
||||||
|
/** Stack size. */
|
||||||
|
ulong stack_size;
|
||||||
|
|
||||||
|
/** Virtual base address. */
|
||||||
|
ulong virt_base;
|
||||||
|
|
||||||
|
/** Stack top virtual address. */
|
||||||
|
ulong virt_stack_top;
|
||||||
|
|
||||||
|
/** Framebuffer virtual address. */
|
||||||
|
ulong virt_fb_buffer;
|
||||||
|
|
||||||
|
/** HULK boot information. */
|
||||||
|
BootInfo bootinfo;
|
||||||
|
}
|
||||||
+329
@@ -0,0 +1,329 @@
|
|||||||
|
/**
|
||||||
|
* HULK Interrupt Manager.
|
||||||
|
*/
|
||||||
|
module hulk.him;
|
||||||
|
|
||||||
|
import hulk.gdt;
|
||||||
|
import ldc.llvmasm;
|
||||||
|
import hulk.fb;
|
||||||
|
import hulk.console;
|
||||||
|
import hulk.klog;
|
||||||
|
import hulk.apic;
|
||||||
|
import hulk.cpu;
|
||||||
|
import hulk.kfont;
|
||||||
|
import hulk.thread;
|
||||||
|
|
||||||
|
/** Number if Interrupt Service Routines. */
|
||||||
|
private enum N_ISRS = 256;
|
||||||
|
|
||||||
|
/** Interrupt stack frame indices. */
|
||||||
|
public static enum
|
||||||
|
{
|
||||||
|
ISF_RBP,
|
||||||
|
ISF_R15,
|
||||||
|
ISF_R14,
|
||||||
|
ISF_R13,
|
||||||
|
ISF_R12,
|
||||||
|
ISF_R11,
|
||||||
|
ISF_R10,
|
||||||
|
ISF_R9,
|
||||||
|
ISF_R8,
|
||||||
|
ISF_RDI,
|
||||||
|
ISF_RSI,
|
||||||
|
ISF_RDX,
|
||||||
|
ISF_RCX,
|
||||||
|
ISF_RBX,
|
||||||
|
ISF_RAX,
|
||||||
|
ISF_ERROR_CODE,
|
||||||
|
ISF_RIP,
|
||||||
|
ISF_CS,
|
||||||
|
ISF_RFLAGS,
|
||||||
|
ISF_RSP,
|
||||||
|
ISF_SS,
|
||||||
|
ISF_COUNT,
|
||||||
|
}
|
||||||
|
|
||||||
|
private static __gshared string[ISF_COUNT] ISF_NAMES = [
|
||||||
|
"RBP",
|
||||||
|
"R15",
|
||||||
|
"R14",
|
||||||
|
"R13",
|
||||||
|
"R12",
|
||||||
|
"R11",
|
||||||
|
"R10",
|
||||||
|
"R9",
|
||||||
|
"R8",
|
||||||
|
"RDI",
|
||||||
|
"RSI",
|
||||||
|
"RDX",
|
||||||
|
"RCX",
|
||||||
|
"RBX",
|
||||||
|
"RAX",
|
||||||
|
"Error Code",
|
||||||
|
"RIP",
|
||||||
|
"CS",
|
||||||
|
"RFLAGS",
|
||||||
|
"RSP",
|
||||||
|
"SS",
|
||||||
|
];
|
||||||
|
|
||||||
|
/** Interrupt IDs. @{ */
|
||||||
|
public enum ulong INT_PAGE_FAULT = 0x0Eu;
|
||||||
|
|
||||||
|
/* The I/O APIC is configured to map IRQ 0 to interrupt 64 (0x40). */
|
||||||
|
public enum ulong INT_APIC_BASE = 0x40u; /* IRQ 0 */
|
||||||
|
public enum ulong INT_APIC_COUNT = 24u;
|
||||||
|
|
||||||
|
public enum ulong INT_APIC_TIMER = 0x70u;
|
||||||
|
public enum ulong INT_APIC_LINT0 = 0x71u;
|
||||||
|
public enum ulong INT_APIC_LINT1 = 0x72u;
|
||||||
|
public enum ulong INT_KERNEL_SWINT = 0x80u;
|
||||||
|
public enum ulong INT_APIC_SPURIOUS = 0xFFu;
|
||||||
|
/** @} */
|
||||||
|
|
||||||
|
static foreach(isr; 0 .. N_ISRS)
|
||||||
|
{
|
||||||
|
mixin(`private extern(C) void isr_`, isr, `();`);
|
||||||
|
}
|
||||||
|
|
||||||
|
private string isrs_list(int index = 0)()
|
||||||
|
{
|
||||||
|
static if (index < 256)
|
||||||
|
{
|
||||||
|
return "&isr_" ~ index.stringof ~ ",\n" ~ isrs_list!(index + 1)();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mixin(`private static __gshared extern(C) void function()[N_ISRS] isrs = [`,
|
||||||
|
isrs_list(),
|
||||||
|
`];`);
|
||||||
|
|
||||||
|
/** Interrupt suspend level (0 when interrupts are enabled). */
|
||||||
|
private __gshared size_t interrupt_suspend_level;
|
||||||
|
|
||||||
|
/** Interrupt nest level (0 when not in an interrupt handler). */
|
||||||
|
private __gshared size_t interrupt_level;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Suspend interrupts.
|
||||||
|
*
|
||||||
|
* This function disables interrupts. Calls can be nested and interrupts will
|
||||||
|
* be enabled only when the outermost pair of
|
||||||
|
* suspend_interrupts()/resume_interrupts() is complete.
|
||||||
|
*/
|
||||||
|
void suspend_interrupts()
|
||||||
|
{
|
||||||
|
if (interrupt_level == 0)
|
||||||
|
{
|
||||||
|
cli();
|
||||||
|
interrupt_suspend_level++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resume interrupts.
|
||||||
|
*
|
||||||
|
* Calls can be nested and interrupts will be enabled only when the outermost
|
||||||
|
* pair of suspend_interrupts()/resume_interrupts() is complete.
|
||||||
|
*/
|
||||||
|
void resume_interrupts()
|
||||||
|
{
|
||||||
|
if (interrupt_level == 0)
|
||||||
|
{
|
||||||
|
interrupt_suspend_level--;
|
||||||
|
if (interrupt_suspend_level == 0)
|
||||||
|
{
|
||||||
|
sti();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Him
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* IDT descriptor entry type.
|
||||||
|
*/
|
||||||
|
struct descriptor_t
|
||||||
|
{
|
||||||
|
ulong[2] descriptor;
|
||||||
|
|
||||||
|
this(T)(size_t index, ulong dpl, bool trap, T offset)
|
||||||
|
{
|
||||||
|
descriptor[0] =
|
||||||
|
0x0000_8E_00_0000_0000u |
|
||||||
|
((cast(ulong)offset & 0xFFFF_0000u) << 32u) |
|
||||||
|
(dpl << 45u) |
|
||||||
|
((trap ? 1uL : 0uL) << 40u) |
|
||||||
|
(Gdt.SELECTOR_KERNEL_CODE << 16u) |
|
||||||
|
(cast(ulong)offset & 0xFFFFu);
|
||||||
|
descriptor[1] = cast(ulong)offset >> 32u;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* IDTR register format.
|
||||||
|
*/
|
||||||
|
struct idtr_t
|
||||||
|
{
|
||||||
|
ushort limit;
|
||||||
|
align(2) ulong offset;
|
||||||
|
}
|
||||||
|
static assert(idtr_t.sizeof == 10u);
|
||||||
|
|
||||||
|
private static __gshared idtr_t idtr;
|
||||||
|
private static __gshared align(0x10) descriptor_t[N_ISRS] idt;
|
||||||
|
static assert(idt.sizeof == (N_ISRS * 16u));
|
||||||
|
|
||||||
|
public static void initialize()
|
||||||
|
{
|
||||||
|
set_isrs();
|
||||||
|
idtr.limit = idt.sizeof - 1u;
|
||||||
|
idtr.offset = cast(ulong)&idt;
|
||||||
|
lidt(&idtr);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void lidt(idtr_t * idtr)
|
||||||
|
{
|
||||||
|
__asm("lidt $0", "*m", idtr);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void set_isrs()
|
||||||
|
{
|
||||||
|
static bool isr_has_error_code(size_t isr)
|
||||||
|
{
|
||||||
|
return (isr == 8) ||
|
||||||
|
((10 <= isr) && (isr <= 14)) ||
|
||||||
|
(isr == 17) ||
|
||||||
|
(isr == 21) ||
|
||||||
|
(isr == 29) ||
|
||||||
|
(isr == 30);
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool isr_is_trap(size_t isr)
|
||||||
|
{
|
||||||
|
return (isr < 32u) && (isr != 2u);
|
||||||
|
}
|
||||||
|
|
||||||
|
__asm("jmp 1f", "");
|
||||||
|
static foreach(isr; 0 .. N_ISRS)
|
||||||
|
{
|
||||||
|
mixin("__asm(`isr_", isr, ":
|
||||||
|
" ~ (isr_has_error_code(isr) ? "" : "pushq $$0") ~ "
|
||||||
|
pushq %rax
|
||||||
|
mov $$", isr, ", %rax
|
||||||
|
jmp isr_common`, ``);");
|
||||||
|
}
|
||||||
|
|
||||||
|
__asm(`isr_common:
|
||||||
|
pushq %rbx
|
||||||
|
pushq %rcx
|
||||||
|
pushq %rdx
|
||||||
|
pushq %rsi
|
||||||
|
pushq %rdi
|
||||||
|
pushq %r8
|
||||||
|
pushq %r9
|
||||||
|
pushq %r10
|
||||||
|
pushq %r11
|
||||||
|
pushq %r12
|
||||||
|
pushq %r13
|
||||||
|
pushq %r14
|
||||||
|
pushq %r15
|
||||||
|
pushq %rbp
|
||||||
|
mov %rsp, %rbp
|
||||||
|
mov %rsp, %rsi
|
||||||
|
and $$0xFFFFFFFFFFFFFFF0, %rsp
|
||||||
|
mov %rsp, %rdx
|
||||||
|
mov %rax, %rdi
|
||||||
|
movabs $$isr, %rax
|
||||||
|
callq *%rax
|
||||||
|
mov %rax, %rsp
|
||||||
|
popq %rbp
|
||||||
|
popq %r15
|
||||||
|
popq %r14
|
||||||
|
popq %r13
|
||||||
|
popq %r12
|
||||||
|
popq %r11
|
||||||
|
popq %r10
|
||||||
|
popq %r9
|
||||||
|
popq %r8
|
||||||
|
popq %rdi
|
||||||
|
popq %rsi
|
||||||
|
popq %rdx
|
||||||
|
popq %rcx
|
||||||
|
popq %rbx
|
||||||
|
popq %rax
|
||||||
|
add $$8, %rsp
|
||||||
|
iretq
|
||||||
|
1:`, ``);
|
||||||
|
|
||||||
|
for (size_t isr = 0; isr < N_ISRS; isr++)
|
||||||
|
{
|
||||||
|
idt[isr] = descriptor_t(isr, 0u, isr_is_trap(isr), isrs[isr]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interrupt service routine.
|
||||||
|
*
|
||||||
|
* This function returns the stack pointer of the thread to execute when
|
||||||
|
* returning from this function.
|
||||||
|
*/
|
||||||
|
public extern(C) ulong * isr(ulong vector, ulong * stack_frame, ulong * rsp)
|
||||||
|
{
|
||||||
|
interrupt_level++;
|
||||||
|
if (interrupt_level == 1)
|
||||||
|
{
|
||||||
|
Thread.current_thread.stack_pointer = stack_frame;
|
||||||
|
}
|
||||||
|
if ((vector >= INT_APIC_BASE) && (vector < (INT_APIC_BASE + INT_APIC_COUNT)))
|
||||||
|
{
|
||||||
|
Apic.isr(vector);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
switch (vector)
|
||||||
|
{
|
||||||
|
case INT_APIC_TIMER:
|
||||||
|
case INT_APIC_LINT0:
|
||||||
|
case INT_APIC_LINT1:
|
||||||
|
case INT_APIC_SPURIOUS:
|
||||||
|
Apic.isr(vector);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case INT_KERNEL_SWINT:
|
||||||
|
Thread.handle_swint(stack_frame);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
Fb.rect(0, 0, Fb.width, Kfont.line_height, 0xCC0000u);
|
||||||
|
Klog.writefln("\n **** Unhandled ISR %u ****", vector);
|
||||||
|
for (size_t i = 0u; i < ISF_COUNT; i++)
|
||||||
|
{
|
||||||
|
Klog.writef(ISF_NAMES[i]);
|
||||||
|
Klog.writefln(" = 0x%x", stack_frame[i]);
|
||||||
|
}
|
||||||
|
Klog.writefln("CR2 = 0x%p", read_cr2());
|
||||||
|
__asm("cli", "");
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
__asm("hlt", "");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Thread.current_thread = Thread.current_thread.list_next;
|
||||||
|
interrupt_level--;
|
||||||
|
if (interrupt_level == 0)
|
||||||
|
{
|
||||||
|
return Thread.current_thread.stack_pointer;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return rsp;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,218 @@
|
|||||||
|
/**
|
||||||
|
* HIPPO, the HOS In-place Physical Page Organizer.
|
||||||
|
*
|
||||||
|
* HIPPO maintains a list of free physical pages "in place", meaning that the
|
||||||
|
* available page itself is used as the linked list entry so separate memory
|
||||||
|
* is not needed to keep track of the available pages.
|
||||||
|
*/
|
||||||
|
module hulk.hippo;
|
||||||
|
|
||||||
|
import hulk.hurl;
|
||||||
|
import hulk.klog;
|
||||||
|
|
||||||
|
struct Hippo
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Linked list node entry for a single page.
|
||||||
|
*/
|
||||||
|
private static struct PhysicalPage
|
||||||
|
{
|
||||||
|
PhysicalPage * next;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Linked list node entry for a region.
|
||||||
|
*/
|
||||||
|
private static struct Region
|
||||||
|
{
|
||||||
|
ulong size;
|
||||||
|
Region * next;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Linked list of free physical pages.
|
||||||
|
*/
|
||||||
|
private static __gshared PhysicalPage * free_pages;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Linked list of free regions.
|
||||||
|
*/
|
||||||
|
private static __gshared Region * regions;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Number of free physical pages.
|
||||||
|
*/
|
||||||
|
private static __gshared size_t m_n_free_pages;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Free a physical page.
|
||||||
|
*
|
||||||
|
* @param phys Physical page address.
|
||||||
|
*/
|
||||||
|
public static void free_page(T)(T phys)
|
||||||
|
{
|
||||||
|
PhysicalPage * pp = cast(PhysicalPage *)phys;
|
||||||
|
pp.next = free_pages;
|
||||||
|
free_pages = pp;
|
||||||
|
m_n_free_pages++;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Allocate a physical page.
|
||||||
|
*
|
||||||
|
* @return Page address, or null if no pages are available.
|
||||||
|
*/
|
||||||
|
public static void * allocate_page()
|
||||||
|
{
|
||||||
|
PhysicalPage * pp;
|
||||||
|
if (free_pages !is null)
|
||||||
|
{
|
||||||
|
pp = free_pages;
|
||||||
|
free_pages = free_pages.next;
|
||||||
|
m_n_free_pages--;
|
||||||
|
}
|
||||||
|
else if (regions !is null)
|
||||||
|
{
|
||||||
|
pp = cast(PhysicalPage *)(cast(ulong)regions + regions.size - PAGE_SIZE);
|
||||||
|
regions.size -= PAGE_SIZE;
|
||||||
|
if (regions.size == 0)
|
||||||
|
{
|
||||||
|
regions = regions.next;
|
||||||
|
}
|
||||||
|
m_n_free_pages--;
|
||||||
|
}
|
||||||
|
return cast(void *)pp;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Free a physical memory region.
|
||||||
|
*/
|
||||||
|
public static void free_region(T)(T start, ulong size)
|
||||||
|
{
|
||||||
|
if (size == PAGE_SIZE)
|
||||||
|
{
|
||||||
|
free_page(start);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (regions is null)
|
||||||
|
{
|
||||||
|
/* The free regions list is empty. Append this new region. */
|
||||||
|
regions = cast(Region *)start;
|
||||||
|
regions.size = size;
|
||||||
|
regions.next = null;
|
||||||
|
}
|
||||||
|
else if (size < regions.size)
|
||||||
|
{
|
||||||
|
/* The new region is smaller than the first one in the list.
|
||||||
|
* Add the new region to the beginning of the list. */
|
||||||
|
Region * new_region = cast(Region *)start;
|
||||||
|
new_region.size = size;
|
||||||
|
new_region.next = regions;
|
||||||
|
regions = new_region;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Find the last free region that is smaller than this one. */
|
||||||
|
Region * region = regions;
|
||||||
|
while (region.next !is null && region.next.size < size)
|
||||||
|
{
|
||||||
|
region = region.next;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Add the new region after this entry. */
|
||||||
|
Region * new_region = cast(Region *)start;
|
||||||
|
new_region.size = size;
|
||||||
|
new_region.next = region.next;
|
||||||
|
region.next = new_region;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_n_free_pages += size >> 12;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Allocate a region with the given size and alignment.
|
||||||
|
*
|
||||||
|
* @param size Region size.
|
||||||
|
* @param alignment Region alignment.
|
||||||
|
*/
|
||||||
|
public static void * allocate_aligned_region(ulong size, ulong alignment)
|
||||||
|
{
|
||||||
|
Region ** regionp = ®ions;
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
Region * region = *regionp;
|
||||||
|
if (region is null)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* The size of the region must be large enough */
|
||||||
|
if (region.size >= size)
|
||||||
|
{
|
||||||
|
/* The alignment contstraint must be satisfied. */
|
||||||
|
if ((cast(ulong)region & (alignment - 1)) == 0)
|
||||||
|
{
|
||||||
|
/* The region start address happens to already be aligned. */
|
||||||
|
if (region.size > size)
|
||||||
|
{
|
||||||
|
/* Region found is larger that needed. */
|
||||||
|
Region * new_region = cast(Region *)(cast(ulong)region + size);
|
||||||
|
new_region.size = region.size - size;
|
||||||
|
new_region.next = region.next;
|
||||||
|
*regionp = new_region;
|
||||||
|
return region;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Region happens to be the size requested. */
|
||||||
|
*regionp = region.next;
|
||||||
|
return region;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* This region is large enough but not aligned. See if an
|
||||||
|
* aligned subregion can be found at the end. */
|
||||||
|
ulong start = (cast(ulong)region + region.size - size) & ~(alignment - 1);
|
||||||
|
if (start > cast(ulong)region)
|
||||||
|
{
|
||||||
|
/* The aligned subregion does fit in this region. */
|
||||||
|
region.size = (start - cast(ulong)region);
|
||||||
|
ulong region_end = cast(ulong)region + region.size;
|
||||||
|
if (region_end > (start + size))
|
||||||
|
{
|
||||||
|
/* There is another region following the aligned
|
||||||
|
* subregion that we must reclaim. */
|
||||||
|
free_region(start + size, region_end - (start + size));
|
||||||
|
}
|
||||||
|
return cast(void *)start;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
regionp = ®ion.next;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the number of free pages.
|
||||||
|
*
|
||||||
|
* @return The number of free pages.
|
||||||
|
*/
|
||||||
|
public static @property size_t n_free_pages()
|
||||||
|
{
|
||||||
|
return m_n_free_pages;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void dump_regions()
|
||||||
|
{
|
||||||
|
Region * region = regions;
|
||||||
|
while (region !is null)
|
||||||
|
{
|
||||||
|
Klog.writefln("HIPPO free region: %p %uKB", region, region.size >> 10);
|
||||||
|
region = region.next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+114
@@ -0,0 +1,114 @@
|
|||||||
|
/**
|
||||||
|
* HULK, the HOS Ultra Light Kernel.
|
||||||
|
*/
|
||||||
|
module hulk.hulk;
|
||||||
|
|
||||||
|
import hulk.header;
|
||||||
|
import hulk.fb;
|
||||||
|
import hulk.console;
|
||||||
|
import hulk.memory;
|
||||||
|
import ldc.attributes;
|
||||||
|
import hulk.kfont;
|
||||||
|
import hulk.klog;
|
||||||
|
import hulk.hurl;
|
||||||
|
import hulk.hippo;
|
||||||
|
import hulk.pci;
|
||||||
|
import hulk.gdt;
|
||||||
|
import hulk.him;
|
||||||
|
import hulk.cpu;
|
||||||
|
import ldc.llvmasm;
|
||||||
|
import hulk.pic;
|
||||||
|
import hulk.acpi;
|
||||||
|
import hulk.apic;
|
||||||
|
import hulk.rtc;
|
||||||
|
import hulk.serial;
|
||||||
|
import hulk.usb;
|
||||||
|
import hulk.pit;
|
||||||
|
import hulk.time;
|
||||||
|
import hulk.test;
|
||||||
|
import hulk.thread;
|
||||||
|
|
||||||
|
extern extern(C) __gshared ubyte _hulk_bss_size;
|
||||||
|
|
||||||
|
@(ldc.attributes.section(".hulk_header"))
|
||||||
|
private __gshared HulkHeader hulk_header = {
|
||||||
|
&_hulk_bss_size, /* hulk_bss_size */
|
||||||
|
&hulk_start, /* entry */
|
||||||
|
Thread.STACK_SIZE, /* stack_size */
|
||||||
|
Hurl.HULK_BASE, /* virt_base */
|
||||||
|
Hurl.HULK_STACK_TOP, /* virt_stack_top */
|
||||||
|
Hurl.HULK_FRAMEBUFFER, /* virt_fb_buffer */
|
||||||
|
};
|
||||||
|
|
||||||
|
private void initialize_cpu()
|
||||||
|
{
|
||||||
|
/* Enable SSE. */
|
||||||
|
/* Turn off CR0.EM and turn on CR0.MP. */
|
||||||
|
write_cr0((read_cr0() & ~CR0_EM) | CR0_MP);
|
||||||
|
/* Set CR4.OSFXSR and CR4.OSXMMEXCPT. */
|
||||||
|
write_cr4(read_cr4() | CR4_OSFXSR | CR4_OSXMMEXCPT);
|
||||||
|
|
||||||
|
/* Enable OSXSAVE. */
|
||||||
|
write_cr4(read_cr4() | CR4_OSXSAVE);
|
||||||
|
|
||||||
|
/* Turn on NXE (no execute enable) flag in the EFER MSR. */
|
||||||
|
wrmsr(MSR_EFER, rdmsr(MSR_EFER) | EFER_NXE);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* HULK entry point.
|
||||||
|
*/
|
||||||
|
void hulk_start()
|
||||||
|
{
|
||||||
|
suspend_interrupts();
|
||||||
|
|
||||||
|
initialize_cpu();
|
||||||
|
Serial.initialize();
|
||||||
|
Gdt.initialize();
|
||||||
|
Him.initialize();
|
||||||
|
Fb.initialize(cast(uint *)Hurl.HULK_FRAMEBUFFER,
|
||||||
|
cast(uint *)hulk_header.bootinfo.fb_buffer1_phys,
|
||||||
|
hulk_header.bootinfo.fb.width,
|
||||||
|
hulk_header.bootinfo.fb.height,
|
||||||
|
hulk_header.bootinfo.fb.stride);
|
||||||
|
Console.initialize();
|
||||||
|
Console.clear();
|
||||||
|
Klog.initialize();
|
||||||
|
|
||||||
|
Klog.writefln("\a5Welcome to HULK, the HOS UltraLight Kernel!");
|
||||||
|
|
||||||
|
Hurl.initialize(&hulk_header);
|
||||||
|
Pic.initialize();
|
||||||
|
Acpi.initialize(hulk_header.bootinfo.acpi_xsdt_phys);
|
||||||
|
Apic.initialize();
|
||||||
|
Rtc.initialize();
|
||||||
|
Pit.initialize();
|
||||||
|
Pci.initialize();
|
||||||
|
Thread.initialize();
|
||||||
|
|
||||||
|
resume_interrupts();
|
||||||
|
|
||||||
|
Usb.initialize();
|
||||||
|
|
||||||
|
/* Run kernel tests. */
|
||||||
|
Test.run();
|
||||||
|
Klog.writefln("\a5HULK Initialization Complete!");
|
||||||
|
// Thread.start(&th);
|
||||||
|
// Time.msleep(1);
|
||||||
|
// Time.msleep(500);
|
||||||
|
// Klog.writefln("Main thread!");
|
||||||
|
/* Idle loop. */
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
hlt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void th()
|
||||||
|
{
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
Klog.writefln("Hello from thread.");
|
||||||
|
Time.msleep(1000);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
SECTIONS
|
||||||
|
{
|
||||||
|
. = 0xFFFFFFFF80000000;
|
||||||
|
_hulk_mem_start = .;
|
||||||
|
_hulk_header_start = .;
|
||||||
|
|
||||||
|
.hulk_header :
|
||||||
|
{
|
||||||
|
KEEP(*(.hulk_header))
|
||||||
|
}
|
||||||
|
|
||||||
|
. = ALIGN(4K);
|
||||||
|
_hulk_header_end = .;
|
||||||
|
_hulk_text_start = .;
|
||||||
|
|
||||||
|
.text :
|
||||||
|
{
|
||||||
|
*(.text)
|
||||||
|
*(.text.*)
|
||||||
|
}
|
||||||
|
|
||||||
|
. = ALIGN(4K);
|
||||||
|
_hulk_text_end = .;
|
||||||
|
_hulk_rodata_start = .;
|
||||||
|
|
||||||
|
.rodata :
|
||||||
|
{
|
||||||
|
*(.rodata)
|
||||||
|
*(.rodata.*)
|
||||||
|
}
|
||||||
|
|
||||||
|
. = ALIGN(4K);
|
||||||
|
|
||||||
|
_hulk_rodata_end = .;
|
||||||
|
_hulk_data_start = .;
|
||||||
|
|
||||||
|
.data :
|
||||||
|
{
|
||||||
|
*(.data)
|
||||||
|
*(.data.*)
|
||||||
|
}
|
||||||
|
|
||||||
|
. = ALIGN(4K);
|
||||||
|
_hulk_data_end = .;
|
||||||
|
_hulk_binary_size = . - _hulk_mem_start;
|
||||||
|
_hulk_bss_start = .;
|
||||||
|
|
||||||
|
.bss :
|
||||||
|
{
|
||||||
|
*(COMMON)
|
||||||
|
*(.bss)
|
||||||
|
*(.bss.*)
|
||||||
|
}
|
||||||
|
|
||||||
|
. = ALIGN(4K);
|
||||||
|
_hulk_bss_end = .;
|
||||||
|
_hulk_mem_end = .;
|
||||||
|
|
||||||
|
_hulk_header_size = _hulk_header_end - _hulk_header_start;
|
||||||
|
_hulk_text_size = _hulk_text_end - _hulk_text_start;
|
||||||
|
_hulk_rodata_size = _hulk_rodata_end - _hulk_rodata_start;
|
||||||
|
_hulk_data_size = _hulk_data_end - _hulk_data_start;
|
||||||
|
_hulk_bss_size = _hulk_bss_end - _hulk_bss_start;
|
||||||
|
_hulk_mem_size = _hulk_mem_end - _hulk_mem_start;
|
||||||
|
}
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
/**
|
||||||
|
* A1 memory allocator.
|
||||||
|
*/
|
||||||
|
module hulk.hurl.a1;
|
||||||
|
|
||||||
|
import hulk.hurl;
|
||||||
|
import hulk.hippo;
|
||||||
|
import hulk.util;
|
||||||
|
import hulk.pagetable;
|
||||||
|
import hulk.memory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The A1 memory allocator is a one-shot memory allocator for kernel memory
|
||||||
|
* that is allocated but never freed.
|
||||||
|
*/
|
||||||
|
struct A1
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Number of bytes allocated in the A1 region so far.
|
||||||
|
*/
|
||||||
|
private static __gshared size_t allocated;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Allocate memory.
|
||||||
|
*
|
||||||
|
* @param size
|
||||||
|
* Size of memory to allocate.
|
||||||
|
*
|
||||||
|
* @return Address of allocated memory. This address will always be aligned
|
||||||
|
* to a multiple of 16 bytes.
|
||||||
|
*/
|
||||||
|
public static void * allocate(size_t size)
|
||||||
|
{
|
||||||
|
/* Round size up to a multiple of 16. */
|
||||||
|
size = round_up_power_2(size, 16u);
|
||||||
|
|
||||||
|
ulong address = Hurl.A1_BASE + allocated;
|
||||||
|
|
||||||
|
ulong current_limit = round_up_power_2(address, PAGE_SIZE);
|
||||||
|
|
||||||
|
allocated += size;
|
||||||
|
|
||||||
|
ulong desired_limit = round_up_power_2(Hurl.A1_BASE + allocated, PAGE_SIZE);
|
||||||
|
|
||||||
|
while (desired_limit > current_limit)
|
||||||
|
{
|
||||||
|
void * page = Hippo.allocate_page();
|
||||||
|
Hurl.map(current_limit, page, PT_WRITABLE);
|
||||||
|
current_limit += PAGE_SIZE;
|
||||||
|
}
|
||||||
|
|
||||||
|
memset64(cast(void *)address, 0, size / 8);
|
||||||
|
|
||||||
|
return cast(void *)address;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Allocate memory to store an instance of a given type.
|
||||||
|
*
|
||||||
|
* @return Address of allocated memory. This address will always be aligned
|
||||||
|
* to a multiple of 16 bytes.
|
||||||
|
*/
|
||||||
|
public static T * allocate(T)()
|
||||||
|
{
|
||||||
|
return cast(T *)allocate(T.sizeof);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,359 @@
|
|||||||
|
/**
|
||||||
|
* HURL, the HOS Unreal Region Locator.
|
||||||
|
*
|
||||||
|
* HURL provides virtual memory management for HULK.
|
||||||
|
*/
|
||||||
|
module hulk.hurl.hurl;
|
||||||
|
|
||||||
|
public import hulk.pagetable;
|
||||||
|
import hulk.cpu;
|
||||||
|
import hulk.hippo;
|
||||||
|
import hulk.memory;
|
||||||
|
import hulk.klog;
|
||||||
|
import hulk.bootinfo;
|
||||||
|
import hulk.header;
|
||||||
|
import hulk.linker_addresses;
|
||||||
|
import hulk.util;
|
||||||
|
|
||||||
|
struct Hurl
|
||||||
|
{
|
||||||
|
/** HULK base address. */
|
||||||
|
enum ulong HULK_BASE = 0xFFFF_FFFF_8000_0000u;
|
||||||
|
|
||||||
|
/** HULK stack top address. */
|
||||||
|
enum ulong HULK_STACK_TOP = 0xFFFF_FFFF_9000_0000u;
|
||||||
|
|
||||||
|
/** HULK framebuffer address. */
|
||||||
|
enum ulong HULK_FRAMEBUFFER = 0xFFFF_FFFF_9000_0000u;
|
||||||
|
|
||||||
|
/** A1 allocator range base address. */
|
||||||
|
enum ulong A1_BASE = 0xFFFF_FFFF_A000_0000u;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pointer to the base page table.
|
||||||
|
*/
|
||||||
|
private static __gshared PageTable * m_pt_base;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build HULK page tables.
|
||||||
|
*
|
||||||
|
* @param physical_address_limit
|
||||||
|
* Limit of physical memory.
|
||||||
|
*/
|
||||||
|
private static void build_page_tables(HulkHeader * header, size_t physical_address_limit)
|
||||||
|
{
|
||||||
|
m_pt_base = allocate_pt();
|
||||||
|
/* Identity map all physical RAM. */
|
||||||
|
map_range(0u,
|
||||||
|
0u,
|
||||||
|
physical_address_limit,
|
||||||
|
PT_WRITABLE | PT_NO_EXECUTE);
|
||||||
|
ulong phys_address = header.bootinfo.hulk_phys;
|
||||||
|
ulong virt_address = HULK_BASE;
|
||||||
|
/* Map HULK header region. */
|
||||||
|
map_range(virt_address,
|
||||||
|
phys_address,
|
||||||
|
LinkerAddresses.hulk_header_size,
|
||||||
|
PT_NO_EXECUTE);
|
||||||
|
phys_address += LinkerAddresses.hulk_header_size;
|
||||||
|
virt_address += LinkerAddresses.hulk_header_size;
|
||||||
|
/* Map HULK text region. */
|
||||||
|
map_range(virt_address,
|
||||||
|
phys_address,
|
||||||
|
LinkerAddresses.hulk_text_size,
|
||||||
|
0u);
|
||||||
|
phys_address += LinkerAddresses.hulk_text_size;
|
||||||
|
virt_address += LinkerAddresses.hulk_text_size;
|
||||||
|
/* Map HULK rodata region. */
|
||||||
|
map_range(virt_address,
|
||||||
|
phys_address,
|
||||||
|
LinkerAddresses.hulk_rodata_size,
|
||||||
|
PT_NO_EXECUTE);
|
||||||
|
phys_address += LinkerAddresses.hulk_rodata_size;
|
||||||
|
virt_address += LinkerAddresses.hulk_rodata_size;
|
||||||
|
/* Map HULK data region. */
|
||||||
|
map_range(virt_address,
|
||||||
|
phys_address,
|
||||||
|
LinkerAddresses.hulk_data_size,
|
||||||
|
PT_WRITABLE | PT_NO_EXECUTE);
|
||||||
|
virt_address += LinkerAddresses.hulk_data_size;
|
||||||
|
/* Map HULK BSS region. */
|
||||||
|
map_range(virt_address,
|
||||||
|
header.bootinfo.bss_phys,
|
||||||
|
LinkerAddresses.hulk_bss_size,
|
||||||
|
PT_WRITABLE | PT_NO_EXECUTE);
|
||||||
|
/* Map HULK stack. */
|
||||||
|
map_range(HULK_STACK_TOP - header.stack_size,
|
||||||
|
header.bootinfo.stack_phys,
|
||||||
|
header.stack_size,
|
||||||
|
PT_WRITABLE | PT_NO_EXECUTE);
|
||||||
|
/* Map device framebuffer. */
|
||||||
|
map_range(HULK_FRAMEBUFFER,
|
||||||
|
cast(ulong)header.bootinfo.fb.buffer,
|
||||||
|
header.bootinfo.fb.height * header.bootinfo.fb.stride * 4u,
|
||||||
|
PT_PAT | PT_WRITABLE | PT_NO_EXECUTE);
|
||||||
|
/* Map framebuffer buffer1. */
|
||||||
|
map_range(cast(ulong)header.bootinfo.fb_buffer1_phys,
|
||||||
|
cast(ulong)header.bootinfo.fb_buffer1_phys,
|
||||||
|
header.bootinfo.fb.height * header.bootinfo.fb.stride * 4u,
|
||||||
|
PT_WRITABLE | PT_NO_EXECUTE);
|
||||||
|
write_cr3(cast(ulong)m_pt_base);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reclaim the pages that were used for the bootloader page tables.
|
||||||
|
*
|
||||||
|
* @param pt
|
||||||
|
* The page table to reclaim pages from.
|
||||||
|
* @param level
|
||||||
|
* Page table level (internally used while recursing the page tables).
|
||||||
|
*
|
||||||
|
* @return Number of pages reclaimed.
|
||||||
|
*/
|
||||||
|
private static size_t reclaim_bootloader_page_table_pages(PageTable * pt, size_t level = 0u)
|
||||||
|
{
|
||||||
|
map(pt, pt, PT_WRITABLE | PT_NO_EXECUTE);
|
||||||
|
size_t reclaimed_pages;
|
||||||
|
for (size_t i = 0u; i < PageTable.N_ENTRIES; i++)
|
||||||
|
{
|
||||||
|
PageTableEntry pte = (*pt)[i];
|
||||||
|
if (pte.present && (level < 2u) && !pte.huge)
|
||||||
|
{
|
||||||
|
/* For the first two levels of page tables, first
|
||||||
|
* recurse and free pages from the lower level page
|
||||||
|
* tables before reclaiming this entry. */
|
||||||
|
reclaimed_pages += reclaim_bootloader_page_table_pages(pte.follow(), level + 1u);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Hippo.free_page(pt);
|
||||||
|
reclaimed_pages++;
|
||||||
|
return reclaimed_pages;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize HURL.
|
||||||
|
*
|
||||||
|
* @param bootinfo HULK boot information structure.
|
||||||
|
*/
|
||||||
|
public static void initialize(HulkHeader * header)
|
||||||
|
{
|
||||||
|
PageTable * bootloader_pt_base = cast(PageTable *)read_cr3();
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Ok, what we do here is iterate through all of the memory map regions
|
||||||
|
* in the bootinfo memory_map array and mark each page that is not in
|
||||||
|
* use in a reserved range as available. Within the available memory
|
||||||
|
* regions, we have to watch out for the following items:
|
||||||
|
* 1) HULK binary (header + text + rodata + data)
|
||||||
|
* 2) Framebuffer
|
||||||
|
* In addition to these ranges, there are also pages for the following
|
||||||
|
* purposes that are not represented in the memory map from the
|
||||||
|
* bootloader:
|
||||||
|
* 3) HULK BSS
|
||||||
|
* 4) HULK stack
|
||||||
|
* 5) bootloader page table pages
|
||||||
|
* 6) framebuffer buffer1
|
||||||
|
*/
|
||||||
|
size_t usable_memory;
|
||||||
|
size_t physical_address_limit;
|
||||||
|
const(size_t) fb_size = round_up_power_2(header.bootinfo.fb.height * header.bootinfo.fb.stride * 4u, PAGE_SIZE);
|
||||||
|
ulong[2][2] reserved_regions = [
|
||||||
|
[header.bootinfo.hulk_phys, LinkerAddresses.hulk_binary_size],
|
||||||
|
[cast(ulong)header.bootinfo.fb.buffer, fb_size],
|
||||||
|
];
|
||||||
|
for (size_t ri = 0u; ri < reserved_regions.length; ri++)
|
||||||
|
{
|
||||||
|
Klog.writefln("Reserved region %p %uKB", reserved_regions[ri][0], reserved_regions[ri][1] >> 10);
|
||||||
|
reserved_regions[ri][1] += reserved_regions[ri][0];
|
||||||
|
}
|
||||||
|
for (size_t bii = 0u; bii < header.bootinfo.memory_map_count; bii++)
|
||||||
|
{
|
||||||
|
if ((header.bootinfo.memory_map[bii].type == BootInfo.MemoryRegion.Type.Bootloader) ||
|
||||||
|
(header.bootinfo.memory_map[bii].type == BootInfo.MemoryRegion.Type.Conventional))
|
||||||
|
{
|
||||||
|
ulong phys = header.bootinfo.memory_map[bii].base;
|
||||||
|
ulong size = header.bootinfo.memory_map[bii].size;
|
||||||
|
ulong phys_end = phys + size;
|
||||||
|
if (phys_end > physical_address_limit)
|
||||||
|
{
|
||||||
|
physical_address_limit = phys_end;
|
||||||
|
}
|
||||||
|
usable_memory += header.bootinfo.memory_map[bii].size;
|
||||||
|
add_region(phys, size, reserved_regions);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Set PAT4 to indicate Write Combining so we can set the PAT bit in a
|
||||||
|
* page table entry to use Write Combining for that page. */
|
||||||
|
wrmsr(MSR_PAT, (rdmsr(MSR_PAT) & 0xFFFF_FF00_FFFF_FFFFu) | 0x0000_0001_0000_0000u);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Now that we have available physical pages to allocate from, we can
|
||||||
|
* build new page tables to replace the bootloader page tables.
|
||||||
|
*/
|
||||||
|
build_page_tables(header, physical_address_limit);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* After we have switched to the newly constructed page tables, we can
|
||||||
|
* iterate through and free the bootloader page tables. They are most
|
||||||
|
* likely already mapped because we just identity mapped every page up
|
||||||
|
* to the physical_address_limit determined above. But just in case the
|
||||||
|
* bootloader used the last pages of RAM for the page table pages, we
|
||||||
|
* will make sure to map them as we traverse them.
|
||||||
|
*/
|
||||||
|
size_t reclaimed_bootloader_page_table_memory = reclaim_bootloader_page_table_pages(bootloader_pt_base) << 10u;
|
||||||
|
usable_memory += reclaimed_bootloader_page_table_memory;
|
||||||
|
|
||||||
|
usable_memory += LinkerAddresses.hulk_bss_size;
|
||||||
|
usable_memory += header.stack_size;
|
||||||
|
usable_memory += fb_size;
|
||||||
|
Klog.writefln("Usable memory: %uKB", usable_memory >> 10u);
|
||||||
|
Klog.writefln("Kernel size: %uKB", (LinkerAddresses.hulk_binary_size + LinkerAddresses.hulk_bss_size + header.stack_size) >> 10u);
|
||||||
|
Klog.writefln("Free pages: %u", Hippo.n_free_pages);
|
||||||
|
// Hippo.dump_regions();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a region of available physical memory.
|
||||||
|
*
|
||||||
|
* @param start Start address of region.
|
||||||
|
* @param size Size of region.
|
||||||
|
* @param reserved_regions List of reserved regions to exclude.
|
||||||
|
*/
|
||||||
|
private static void add_region(ulong start, ulong size, ulong[2][] reserved_regions)
|
||||||
|
{
|
||||||
|
ulong end = start + size;
|
||||||
|
bool split;
|
||||||
|
|
||||||
|
/* Skip empty regions. */
|
||||||
|
if (size == 0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t rri = 0; rri < reserved_regions.length; rri++)
|
||||||
|
{
|
||||||
|
/* Skip this region if it is entirely within a reserved region. */
|
||||||
|
if (reserved_regions[rri][0] <= start && reserved_regions[rri][1] >= end)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Check if the region is partly reserved but the beginning is free. */
|
||||||
|
if (reserved_regions[rri][0] > start && reserved_regions[rri][0] < end)
|
||||||
|
{
|
||||||
|
split = true;
|
||||||
|
add_region(start, reserved_regions[rri][0] - start, reserved_regions);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Check if the region is partly reserved but the end is free. */
|
||||||
|
if (reserved_regions[rri][1] > start && reserved_regions[rri][1] < end)
|
||||||
|
{
|
||||||
|
split = true;
|
||||||
|
add_region(reserved_regions[rri][1], end - reserved_regions[rri][1], reserved_regions);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (split)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* If a region makes it here, it did not overlap with any reserved
|
||||||
|
* region, so the entire region can be used. */
|
||||||
|
// Klog.writefln("HURL: freeing region %p %uKB", start, size >> 10);
|
||||||
|
Hippo.free_region(start, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void map(ulong virtual, ulong physical, ulong flags)
|
||||||
|
{
|
||||||
|
PageTable * pt = m_pt_base;
|
||||||
|
for (size_t level = 0; level < 4u; level++)
|
||||||
|
{
|
||||||
|
PageTableEntry * ppte = pt.entry(virtual, level);
|
||||||
|
if (level < 3u)
|
||||||
|
{
|
||||||
|
PageTableEntry pte = *ppte;
|
||||||
|
if (pte.present)
|
||||||
|
{
|
||||||
|
pt = pte.follow();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
PageTable * next_pt = allocate_pt();
|
||||||
|
*ppte = PageTableEntry(next_pt, PT_WRITABLE | PT_PRESENT);
|
||||||
|
pt = next_pt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
*ppte = PageTableEntry(cast(ulong)physical, flags | PT_PRESENT);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void map(T, U)(T virtual, U physical, ulong flags)
|
||||||
|
{
|
||||||
|
map(cast(ulong)virtual, cast(ulong)physical, flags);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void map_range(ulong virtual, ulong physical, size_t length, ulong flags)
|
||||||
|
{
|
||||||
|
size_t end = virtual + length;
|
||||||
|
while (virtual < end)
|
||||||
|
{
|
||||||
|
map(virtual, physical, flags);
|
||||||
|
virtual += PAGE_SIZE;
|
||||||
|
physical += PAGE_SIZE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void map_range(T, U)(T virtual, U physical, size_t length, ulong flags)
|
||||||
|
{
|
||||||
|
map_range(cast(ulong)virtual, cast(ulong)physical, length, flags);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void identity_map_range(ulong address, size_t length, ulong flags)
|
||||||
|
{
|
||||||
|
size_t end = address + length;
|
||||||
|
for (size_t page = address & 0xFFFF_FFFF_FFFF_F000u; page < end; page += PAGE_SIZE)
|
||||||
|
{
|
||||||
|
map(page, page, flags);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void identity_map_range(T)(T address, size_t length, ulong flags)
|
||||||
|
{
|
||||||
|
identity_map_range(cast(ulong)address, length, flags);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void debug_lookup(void * address)
|
||||||
|
{
|
||||||
|
Klog.writefln("Debugging page table lookup of 0x%x", address);
|
||||||
|
PageTable * pt = m_pt_base;
|
||||||
|
for (size_t level = 0; level < 4u; level++)
|
||||||
|
{
|
||||||
|
PageTableEntry pte = *pt.entry(address, level);
|
||||||
|
Klog.writefln("Level %u, index %u, entry = 0x%x", level, pt.index(address, level), pte);
|
||||||
|
if (pte.present)
|
||||||
|
{
|
||||||
|
pt = pte.follow();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (pte.huge)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static PageTable * allocate_pt()
|
||||||
|
{
|
||||||
|
PageTable * pt = cast(PageTable *)Hippo.allocate_page();
|
||||||
|
memset64(pt, 0u, PAGE_SIZE / 8u);
|
||||||
|
return pt;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
module hulk.hurl;
|
||||||
|
|
||||||
|
public import hulk.hurl.hurl;
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
/**
|
||||||
|
* HULK Kernel Log buffer.
|
||||||
|
*/
|
||||||
|
module hulk.klog;
|
||||||
|
|
||||||
|
import core.stdc.stdarg;
|
||||||
|
import hulk.console;
|
||||||
|
static import hulk.writef;
|
||||||
|
import hulk.cpu;
|
||||||
|
|
||||||
|
struct Klog
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Kernel buffer size log.
|
||||||
|
* 16 gives a kernel buffer size of 64KB.
|
||||||
|
*/
|
||||||
|
private enum size_t KLOG_SIZE_LOG = 16u;
|
||||||
|
|
||||||
|
/** Kernel buffer size. */
|
||||||
|
private enum size_t KLOG_SIZE = 1u << KLOG_SIZE_LOG;
|
||||||
|
|
||||||
|
/** Kernel log buffer. */
|
||||||
|
private static __gshared align(4096) ubyte[KLOG_SIZE] klog_buffer;
|
||||||
|
|
||||||
|
/** Write index in the kernel log buffer. */
|
||||||
|
private static __gshared size_t klog_index;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize the klog module.
|
||||||
|
*/
|
||||||
|
public static void initialize()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write a formatted string to the kernel log.
|
||||||
|
*
|
||||||
|
* @param s Format string.
|
||||||
|
* @param args Variable arguments structure.
|
||||||
|
*/
|
||||||
|
public static void writef(string s, va_list args)
|
||||||
|
{
|
||||||
|
hulk.writef.writefv(function void(ubyte ch) {
|
||||||
|
Console.write(ch);
|
||||||
|
}, s, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write a formatted string to the kernel log.
|
||||||
|
*
|
||||||
|
* @param s Format string.
|
||||||
|
*/
|
||||||
|
public static extern (C) void writef(string s, ...)
|
||||||
|
{
|
||||||
|
va_list args;
|
||||||
|
va_start(args, s);
|
||||||
|
writef(s, args);
|
||||||
|
va_end(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write a formatted string and newline to the kernel log.
|
||||||
|
*
|
||||||
|
* @param s Format string.
|
||||||
|
*/
|
||||||
|
public static extern (C) void writefln(string s, ...)
|
||||||
|
{
|
||||||
|
va_list args;
|
||||||
|
va_start(args, s);
|
||||||
|
writef(s, args);
|
||||||
|
writef("\n", args);
|
||||||
|
va_end(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Print a fatal kernel error and loop forever.
|
||||||
|
*/
|
||||||
|
public static void fatal_error(T...)(T args)
|
||||||
|
{
|
||||||
|
writef("FATAL ERROR: ");
|
||||||
|
writefln(args);
|
||||||
|
cli();
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,139 @@
|
|||||||
|
/**
|
||||||
|
* This module provides access to linker-defined symbols.
|
||||||
|
*/
|
||||||
|
module hulk.linker_addresses;
|
||||||
|
|
||||||
|
private extern extern(C) __gshared ubyte _hulk_header_start;
|
||||||
|
private extern extern(C) __gshared ubyte _hulk_header_end;
|
||||||
|
private extern extern(C) __gshared ubyte _hulk_header_size;
|
||||||
|
|
||||||
|
private extern extern(C) __gshared ubyte _hulk_text_start;
|
||||||
|
private extern extern(C) __gshared ubyte _hulk_text_end;
|
||||||
|
private extern extern(C) __gshared ubyte _hulk_text_size;
|
||||||
|
|
||||||
|
private extern extern(C) __gshared ubyte _hulk_rodata_start;
|
||||||
|
private extern extern(C) __gshared ubyte _hulk_rodata_end;
|
||||||
|
private extern extern(C) __gshared ubyte _hulk_rodata_size;
|
||||||
|
|
||||||
|
private extern extern(C) __gshared ubyte _hulk_data_start;
|
||||||
|
private extern extern(C) __gshared ubyte _hulk_data_end;
|
||||||
|
private extern extern(C) __gshared ubyte _hulk_data_size;
|
||||||
|
|
||||||
|
private extern extern(C) __gshared ubyte _hulk_bss_start;
|
||||||
|
private extern extern(C) __gshared ubyte _hulk_bss_end;
|
||||||
|
private extern extern(C) __gshared ubyte _hulk_bss_size;
|
||||||
|
|
||||||
|
/* hulk_binary includes header, text, rodata, and data */
|
||||||
|
private extern extern(C) __gshared ubyte _hulk_binary_size;
|
||||||
|
|
||||||
|
/* hulk_mem includes header, text, rodata, data, and bss */
|
||||||
|
private extern extern(C) __gshared ubyte _hulk_mem_start;
|
||||||
|
private extern extern(C) __gshared ubyte _hulk_mem_end;
|
||||||
|
private extern extern(C) __gshared ubyte _hulk_mem_size;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This struct provides access to linker-defined symbols.
|
||||||
|
*/
|
||||||
|
public struct LinkerAddresses
|
||||||
|
{
|
||||||
|
public static @property ulong hulk_header_start()
|
||||||
|
{
|
||||||
|
return cast(ulong)&_hulk_header_start;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @property ulong hulk_header_end()
|
||||||
|
{
|
||||||
|
return cast(ulong)&_hulk_header_end;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @property ulong hulk_header_size()
|
||||||
|
{
|
||||||
|
return cast(ulong)&_hulk_header_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static @property ulong hulk_text_start()
|
||||||
|
{
|
||||||
|
return cast(ulong)&_hulk_text_start;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @property ulong hulk_text_end()
|
||||||
|
{
|
||||||
|
return cast(ulong)&_hulk_text_end;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @property ulong hulk_text_size()
|
||||||
|
{
|
||||||
|
return cast(ulong)&_hulk_text_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static @property ulong hulk_rodata_start()
|
||||||
|
{
|
||||||
|
return cast(ulong)&_hulk_rodata_start;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @property ulong hulk_rodata_end()
|
||||||
|
{
|
||||||
|
return cast(ulong)&_hulk_rodata_end;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @property ulong hulk_rodata_size()
|
||||||
|
{
|
||||||
|
return cast(ulong)&_hulk_rodata_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static @property ulong hulk_data_start()
|
||||||
|
{
|
||||||
|
return cast(ulong)&_hulk_data_start;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @property ulong hulk_data_end()
|
||||||
|
{
|
||||||
|
return cast(ulong)&_hulk_data_end;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @property ulong hulk_data_size()
|
||||||
|
{
|
||||||
|
return cast(ulong)&_hulk_data_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static @property ulong hulk_bss_start()
|
||||||
|
{
|
||||||
|
return cast(ulong)&_hulk_bss_start;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @property ulong hulk_bss_end()
|
||||||
|
{
|
||||||
|
return cast(ulong)&_hulk_bss_end;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @property ulong hulk_bss_size()
|
||||||
|
{
|
||||||
|
return cast(ulong)&_hulk_bss_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static @property ulong hulk_binary_size()
|
||||||
|
{
|
||||||
|
return cast(ulong)&_hulk_binary_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static @property ulong hulk_mem_start()
|
||||||
|
{
|
||||||
|
return cast(ulong)&_hulk_mem_start;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @property ulong hulk_mem_end()
|
||||||
|
{
|
||||||
|
return cast(ulong)&_hulk_mem_end;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static @property ulong hulk_mem_size()
|
||||||
|
{
|
||||||
|
return cast(ulong)&_hulk_mem_size;
|
||||||
|
}
|
||||||
|
}
|
||||||
+100
@@ -0,0 +1,100 @@
|
|||||||
|
/**
|
||||||
|
* Linked list functionality.
|
||||||
|
*/
|
||||||
|
module hulk.list;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Linked list mixin.
|
||||||
|
*/
|
||||||
|
mixin template List(T)
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Next item in linked list.
|
||||||
|
*/
|
||||||
|
T * list_next;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Previous item in linked list.
|
||||||
|
*/
|
||||||
|
T * list_prev;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize linked list fields.
|
||||||
|
*/
|
||||||
|
public void list_init()
|
||||||
|
{
|
||||||
|
list_next = &this;
|
||||||
|
list_prev = &this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add this item after another in a list.
|
||||||
|
*/
|
||||||
|
public void list_insert_after(T * item)
|
||||||
|
{
|
||||||
|
list_next = item.list_next;
|
||||||
|
list_prev = item;
|
||||||
|
item.list_next = &this;
|
||||||
|
list_next.list_prev = &this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add this item before another in a list.
|
||||||
|
*/
|
||||||
|
public void list_insert_before(T * item)
|
||||||
|
{
|
||||||
|
list_next = item;
|
||||||
|
list_prev = item.list_prev;
|
||||||
|
item.list_prev = &this;
|
||||||
|
list_prev.list_next = &this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove this item from the list.
|
||||||
|
*/
|
||||||
|
public void list_remove()
|
||||||
|
{
|
||||||
|
list_next.list_prev = list_prev;
|
||||||
|
list_prev.list_next = list_next;
|
||||||
|
list_init();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the number of items in this list.
|
||||||
|
*/
|
||||||
|
public @property size_t list_count() const
|
||||||
|
{
|
||||||
|
size_t count = 1;
|
||||||
|
const(T) * item = &this;
|
||||||
|
while (item.list_next != &this)
|
||||||
|
{
|
||||||
|
count++;
|
||||||
|
item = item.list_next;
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Allow foreach iteration through this linked list.
|
||||||
|
*/
|
||||||
|
public int opApply(scope int delegate(T *) dg)
|
||||||
|
{
|
||||||
|
T * item = &this;
|
||||||
|
T * last = item.list_prev;
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
T * next = item.list_next;
|
||||||
|
int result = dg(item);
|
||||||
|
if (result != 0)
|
||||||
|
{
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
if (item == last)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
item = next;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
/**
|
||||||
|
* Memory-related functions.
|
||||||
|
*/
|
||||||
|
module hulk.memory;
|
||||||
|
|
||||||
|
import ldc.llvmasm;
|
||||||
|
|
||||||
|
void memset(void * dest, ubyte v, size_t n)
|
||||||
|
{
|
||||||
|
__asm("rep stosb",
|
||||||
|
"{al},{rcx},{rdi},~{rcx},~{rdi},~{memory}",
|
||||||
|
v, n, dest);
|
||||||
|
}
|
||||||
|
|
||||||
|
void memset16(void * dest, ushort v, size_t n)
|
||||||
|
{
|
||||||
|
__asm("rep stosw",
|
||||||
|
"{ax},{rcx},{rdi},~{rcx},~{rdi},~{memory}",
|
||||||
|
v, n, dest);
|
||||||
|
}
|
||||||
|
|
||||||
|
void memset32(void * dest, uint v, size_t n)
|
||||||
|
{
|
||||||
|
__asm("rep stosl",
|
||||||
|
"{eax},{rcx},{rdi},~{rcx},~{rdi},~{memory}",
|
||||||
|
v, n, dest);
|
||||||
|
}
|
||||||
|
|
||||||
|
void memset64(void * dest, ulong v, size_t n)
|
||||||
|
{
|
||||||
|
__asm("rep stosq",
|
||||||
|
"{rax},{rcx},{rdi},~{rcx},~{rdi},~{memory}",
|
||||||
|
v, n, dest);
|
||||||
|
}
|
||||||
|
|
||||||
|
void memcpy(void * dest, const(void) * src, size_t n)
|
||||||
|
{
|
||||||
|
__asm("rep movsb",
|
||||||
|
"{rcx},{rsi},{rdi},~{rcx},~{rsi},~{rdi},~{memory}",
|
||||||
|
n, src, dest);
|
||||||
|
}
|
||||||
|
|
||||||
|
void memcpy16(void * dest, const(void) * src, size_t n)
|
||||||
|
{
|
||||||
|
__asm("rep movsw",
|
||||||
|
"{rcx},{rsi},{rdi},~{rcx},~{rsi},~{rdi},~{memory}",
|
||||||
|
n, src, dest);
|
||||||
|
}
|
||||||
|
|
||||||
|
void memcpy32(void * dest, const(void) * src, size_t n)
|
||||||
|
{
|
||||||
|
__asm("rep movsl",
|
||||||
|
"{rcx},{rsi},{rdi},~{rcx},~{rsi},~{rdi},~{memory}",
|
||||||
|
n, src, dest);
|
||||||
|
}
|
||||||
|
|
||||||
|
void memcpy64(void * dest, const(void) * src, size_t n)
|
||||||
|
{
|
||||||
|
__asm("rep movsq",
|
||||||
|
"{rcx},{rsi},{rdi},~{rcx},~{rsi},~{rdi},~{memory}",
|
||||||
|
n, src, dest);
|
||||||
|
}
|
||||||
@@ -0,0 +1,70 @@
|
|||||||
|
/**
|
||||||
|
* MTRR (Memory Type Range Register) support for HULK.
|
||||||
|
*/
|
||||||
|
module hulk.mtrr;
|
||||||
|
|
||||||
|
import hulk.cpu;
|
||||||
|
import hulk.klog;
|
||||||
|
|
||||||
|
enum ulong MTRR_TYPE_UC = 0u; /* Uncacheable */
|
||||||
|
enum ulong MTRR_TYPE_WC = 1u; /* Write-Combining */
|
||||||
|
enum ulong MTRR_TYPE_WT = 4u; /* Writethrough */
|
||||||
|
enum ulong MTRR_TYPE_WP = 5u; /* Write-Protect */
|
||||||
|
enum ulong MTRR_TYPE_WB = 6u; /* Writeback */
|
||||||
|
|
||||||
|
enum uint MSR_MTRRCAP = 0xFEu;
|
||||||
|
enum uint MSR_MTRRDEFTYPE = 0x2FFu;
|
||||||
|
enum uint MSR_MTRRPHYSBASE0 = 0x200u;
|
||||||
|
enum uint MSR_MTRRPHYSMASK0 = 0x201u;
|
||||||
|
|
||||||
|
enum ulong MTRRCAP_WC = 0x400u;
|
||||||
|
enum ulong MTRRCAP_FIX = 0x100u;
|
||||||
|
enum ulong MTRRCAP_VCNT_MASK = 0xFFu;
|
||||||
|
|
||||||
|
enum ulong MTRRDEFTYPE_E = 0x800u;
|
||||||
|
enum ulong MTRRDEFTYPE_FE = 0x400u;
|
||||||
|
enum ulong MTRRDEFTYPE_TYPE_MASK = 0xFFu;
|
||||||
|
|
||||||
|
enum ulong MTRRPHYSBASE_PHYSBASE_MASK = 0x000F_FFFF_FFFF_F000u;
|
||||||
|
enum ulong MTRRPHYSBASE_TYPE_MASK = 0xFFu;
|
||||||
|
|
||||||
|
enum ulong MTRRPHYSMASK_PHYSMASK_MASK = 0x000F_FFFF_FFFF_F000u;
|
||||||
|
enum ulong MTRRPHYSMASK_VALID = 0x800u;
|
||||||
|
|
||||||
|
struct Mtrr
|
||||||
|
{
|
||||||
|
public static void initialize()
|
||||||
|
{
|
||||||
|
uint ebx;
|
||||||
|
uint ecx;
|
||||||
|
uint edx;
|
||||||
|
cpuid1(&ebx, &ecx, &edx);
|
||||||
|
if ((edx & CPUID_1_EDX_MTRR) == 0u)
|
||||||
|
{
|
||||||
|
Klog.writefln("CPU does not support MTRR");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const(ulong) mtrrcap = rdmsr(MSR_MTRRCAP);
|
||||||
|
const(ulong) mtrrdeftype = rdmsr(MSR_MTRRDEFTYPE);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ulong get_phys_base(uint n)
|
||||||
|
{
|
||||||
|
return rdmsr(MSR_MTRRPHYSBASE0 + 2u * n);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void set_phys_base(uint n, ulong physbase)
|
||||||
|
{
|
||||||
|
wrmsr(MSR_MTRRPHYSBASE0 + 2u * n, physbase);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ulong get_phys_mask(uint n)
|
||||||
|
{
|
||||||
|
return rdmsr(MSR_MTRRPHYSMASK0 + 2u * n);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void set_phys_mask(uint n, ulong physmask)
|
||||||
|
{
|
||||||
|
wrmsr(MSR_MTRRPHYSMASK0 + 2u * n, physmask);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,194 @@
|
|||||||
|
/**
|
||||||
|
* Page table structures.
|
||||||
|
*/
|
||||||
|
module hulk.pagetable;
|
||||||
|
|
||||||
|
/** Page table entry attributes. @{ */
|
||||||
|
enum ulong PT_PRESENT = 0x1u;
|
||||||
|
enum ulong PT_WRITABLE = 0x2u;
|
||||||
|
enum ulong PT_USER = 0x4u;
|
||||||
|
enum ulong PT_WRITE_THROUGH = 0x8u;
|
||||||
|
enum ulong PT_DISABLE_CACHE = 0x10u;
|
||||||
|
enum ulong PT_ACCESSED = 0x20u;
|
||||||
|
enum ulong PT_DIRTY = 0x40u;
|
||||||
|
enum ulong PT_PAT = 0x80u; /* Page table entry bit. */
|
||||||
|
enum ulong PT_HUGE_PAGE = 0x80u; /* Page directory entry bit. */
|
||||||
|
enum ulong PT_GLOBAL = 0x100u;
|
||||||
|
enum ulong PT_NO_EXECUTE = 0x8000_0000_0000_0000u;
|
||||||
|
/** @} */
|
||||||
|
|
||||||
|
/** Page size in bytes. */
|
||||||
|
enum ulong PAGE_SIZE = 4096u;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Structure that represents a page table entry.
|
||||||
|
*/
|
||||||
|
struct PageTableEntry
|
||||||
|
{
|
||||||
|
/** The raw page table entry is a 64-bit ulong. */
|
||||||
|
public ulong entry;
|
||||||
|
alias entry this;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct a page table entry.
|
||||||
|
*
|
||||||
|
* @param address
|
||||||
|
* Address pointed to by the page table entry (ulong or pointer).
|
||||||
|
* @param flags
|
||||||
|
* Page table flags (ORed set of PT_*).
|
||||||
|
*/
|
||||||
|
this(T)(T address, ulong flags = 0u)
|
||||||
|
{
|
||||||
|
entry = cast(ulong)address | flags;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return whether the page is present.
|
||||||
|
*/
|
||||||
|
public @property bool present() const
|
||||||
|
{
|
||||||
|
return (entry & PT_PRESENT) != 0u;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return whether the page is writable.
|
||||||
|
*/
|
||||||
|
public @property bool writable() const
|
||||||
|
{
|
||||||
|
return (entry & PT_WRITABLE) != 0u;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return whether the page is a user page.
|
||||||
|
*/
|
||||||
|
public @property bool user() const
|
||||||
|
{
|
||||||
|
return (entry & PT_USER) != 0u;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return whether the page is write-through.
|
||||||
|
*/
|
||||||
|
public @property bool write_through() const
|
||||||
|
{
|
||||||
|
return (entry & PT_WRITE_THROUGH) != 0u;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return whether the page has disable cache set.
|
||||||
|
*/
|
||||||
|
public @property bool disable_cache() const
|
||||||
|
{
|
||||||
|
return (entry & PT_DISABLE_CACHE) != 0u;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return whether the page is huge.
|
||||||
|
*/
|
||||||
|
public @property bool huge() const
|
||||||
|
{
|
||||||
|
return (entry & PT_HUGE_PAGE) != 0u;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return whether the page is global.
|
||||||
|
*/
|
||||||
|
public @property bool global() const
|
||||||
|
{
|
||||||
|
return (entry & PT_GLOBAL) != 0u;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return whether the page is no-execute.
|
||||||
|
*/
|
||||||
|
public @property bool no_execute() const
|
||||||
|
{
|
||||||
|
return (entry & PT_NO_EXECUTE) != 0u;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return whether the page is dirty.
|
||||||
|
*/
|
||||||
|
public @property bool dirty() const
|
||||||
|
{
|
||||||
|
return (entry & PT_DIRTY) != 0u;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return whether the page is accessed.
|
||||||
|
*/
|
||||||
|
public @property bool accessed() const
|
||||||
|
{
|
||||||
|
return (entry & PT_ACCESSED) != 0u;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Follow the page table entry to the next page table it points to.
|
||||||
|
*/
|
||||||
|
public PageTable * follow() const
|
||||||
|
{
|
||||||
|
return cast(PageTable *)(entry & 0x7FFF_FFFF_FFFF_F000u);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
static assert(PageTableEntry.sizeof == 8u);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Structure that represents a page table.
|
||||||
|
*/
|
||||||
|
struct PageTable
|
||||||
|
{
|
||||||
|
/** Number of page table entries in a page table. */
|
||||||
|
enum size_t N_ENTRIES = 512u;
|
||||||
|
|
||||||
|
/** Page table entries. */
|
||||||
|
private PageTableEntry[N_ENTRIES] entries;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Access the PageTableEntry for the given address and page table level.
|
||||||
|
*
|
||||||
|
* @param address
|
||||||
|
* Address to look up the page table entry (ulong or pointer).
|
||||||
|
* @param level
|
||||||
|
* Page table level (0-4).
|
||||||
|
*/
|
||||||
|
public PageTableEntry * entry(T)(T address, ulong level)
|
||||||
|
{
|
||||||
|
return &entries[index(cast(ulong)address, level)];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine the index into the page table for the given address and page
|
||||||
|
* table level.
|
||||||
|
*
|
||||||
|
* @param address
|
||||||
|
* Address to look up the page table entry (ulong or pointer).
|
||||||
|
* @param level
|
||||||
|
* Page table level (0-4).
|
||||||
|
*/
|
||||||
|
public ulong index(T)(T address, ulong level)
|
||||||
|
{
|
||||||
|
return (cast(ulong)address >> (39u - (9u * level))) & 0x1FFu;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Access a page table entry by linear index.
|
||||||
|
*
|
||||||
|
* @param index
|
||||||
|
* Page table index (0-511).
|
||||||
|
*
|
||||||
|
* @return PageTableEntry at the given index.
|
||||||
|
*/
|
||||||
|
public PageTableEntry opIndex(size_t index)
|
||||||
|
{
|
||||||
|
return entries[index];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
static assert(PageTable.sizeof == 4096u);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the base address of the page containing the given address.
|
||||||
|
*/
|
||||||
|
T page_address(T)(T address)
|
||||||
|
{
|
||||||
|
return cast(T)(cast(ulong)address & ~(PAGE_SIZE - 1u));
|
||||||
|
}
|
||||||
+452
@@ -0,0 +1,452 @@
|
|||||||
|
/**
|
||||||
|
* PCI (Peripheral Component Interconnect) functionality.
|
||||||
|
*/
|
||||||
|
module hulk.pci;
|
||||||
|
|
||||||
|
import hulk.cpu;
|
||||||
|
import hulk.klog;
|
||||||
|
import hulk.hurl;
|
||||||
|
import hulk.hurl.a1;
|
||||||
|
import hulk.range;
|
||||||
|
import hulk.usb.xhci;
|
||||||
|
import hulk.acpi;
|
||||||
|
import hulk.list;
|
||||||
|
|
||||||
|
struct Pci
|
||||||
|
{
|
||||||
|
/** IO address for PCI configuration address. */
|
||||||
|
enum IO_ADDR_CONFIG_ADDRESS = 0xCF8u;
|
||||||
|
|
||||||
|
/** IO address for PCI configuration data. */
|
||||||
|
enum IO_ADDR_CONFIG_DATA = 0xCFCu;
|
||||||
|
|
||||||
|
/** Maximum number of devices on a bus. */
|
||||||
|
enum MAX_DEVICES_PER_BUS = 32;
|
||||||
|
|
||||||
|
/** Maximum number of functions on a devices. */
|
||||||
|
enum MAX_FUNCTIONS_PER_DEVICE = 8;
|
||||||
|
|
||||||
|
/** XHCI controller device type. */
|
||||||
|
enum XHCI_CONTROLLER = Type(0x0Cu, 0x03u, 0x30u);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* MCFG ACPI table structure.
|
||||||
|
*/
|
||||||
|
static struct MCFG
|
||||||
|
{
|
||||||
|
/* PCI Segment Group Memory Area Descriptor. */
|
||||||
|
static struct SGMADesc
|
||||||
|
{
|
||||||
|
uint[2] base_address_a;
|
||||||
|
ushort pci_segment_group_number;
|
||||||
|
ubyte start_pci_bus_number;
|
||||||
|
ubyte end_pci_bus_number;
|
||||||
|
uint _reserved;
|
||||||
|
|
||||||
|
public @property ulong base_address() const
|
||||||
|
{
|
||||||
|
return base_address_a[0] | (cast(ulong)base_address_a[1] << 32);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
static assert(SGMADesc.sizeof == 16);
|
||||||
|
static assert(SGMADesc.alignof == 4);
|
||||||
|
|
||||||
|
Acpi.Header header;
|
||||||
|
uint[2] _reserved;
|
||||||
|
SGMADesc[0] sgma_descs;
|
||||||
|
|
||||||
|
size_t n_sgma_descs()
|
||||||
|
{
|
||||||
|
return (header.length - MCFG.sgma_descs.offsetof) / MCFG.SGMADesc.sizeof;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* PCI device address (bus number, device number, function number).
|
||||||
|
*/
|
||||||
|
struct Address
|
||||||
|
{
|
||||||
|
uint address;
|
||||||
|
alias address this;
|
||||||
|
|
||||||
|
this(ubyte bus_nr, ubyte device_nr, ubyte function_nr)
|
||||||
|
{
|
||||||
|
this.address = (bus_nr << 16u) | (device_nr << 8u) | function_nr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bus number (0-255).
|
||||||
|
*/
|
||||||
|
public @property ubyte bus_nr() const
|
||||||
|
{
|
||||||
|
return cast(ubyte)(address >> 16u);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Device number (0-31).
|
||||||
|
*/
|
||||||
|
public @property ubyte device_nr() const
|
||||||
|
{
|
||||||
|
return cast(ubyte)(address >> 8u);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Function number (0-7).
|
||||||
|
*/
|
||||||
|
public @property ubyte function_nr() const
|
||||||
|
{
|
||||||
|
return cast(ubyte)address;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* PCI device type (class ID, subclass ID, interface ID).
|
||||||
|
*/
|
||||||
|
struct Type
|
||||||
|
{
|
||||||
|
uint type;
|
||||||
|
alias type this;
|
||||||
|
|
||||||
|
this(ubyte class_id, ubyte subclass_id, ubyte interface_id)
|
||||||
|
{
|
||||||
|
this.type = (class_id << 16u) | (subclass_id << 8u) | interface_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class ID.
|
||||||
|
*/
|
||||||
|
public @property ubyte class_id() const
|
||||||
|
{
|
||||||
|
return cast(ubyte)(type >> 16u);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Subclass ID.
|
||||||
|
*/
|
||||||
|
public @property ubyte subclass_id() const
|
||||||
|
{
|
||||||
|
return cast(ubyte)(type >> 8u);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface ID.
|
||||||
|
*/
|
||||||
|
public @property ubyte interface_id() const
|
||||||
|
{
|
||||||
|
return cast(ubyte)type;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* PCI device object.
|
||||||
|
*/
|
||||||
|
static struct Device
|
||||||
|
{
|
||||||
|
Configuration * config;
|
||||||
|
Address address;
|
||||||
|
Type type;
|
||||||
|
bool multifunction;
|
||||||
|
Range[6] memory_ranges;
|
||||||
|
|
||||||
|
mixin List!Device;
|
||||||
|
|
||||||
|
void initialize(Address address, Configuration * config)
|
||||||
|
{
|
||||||
|
list_init();
|
||||||
|
this.config = config;
|
||||||
|
this.address = address;
|
||||||
|
type = Type(config.class_id, config.subclass_id, config.interface_id);
|
||||||
|
Klog.writefln("Found PCI device %04x:%04x (%02x:%02x:%02x) at %02u:%02u.%u",
|
||||||
|
config.vendor_id, config.device_id,
|
||||||
|
type.class_id, type.subclass_id, type.interface_id,
|
||||||
|
address.bus_nr, address.device_nr, address.function_nr);
|
||||||
|
multifunction = (address.function_nr == 0u) && ((config.header_type & 0x80u) != 0u);
|
||||||
|
ubyte header_type = config.header_type & 0x7Fu;
|
||||||
|
|
||||||
|
if (header_type == 0u)
|
||||||
|
{
|
||||||
|
map_memory_regions();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void map_memory_regions()
|
||||||
|
{
|
||||||
|
size_t range_index;
|
||||||
|
uint[2] r;
|
||||||
|
uint[2] s;
|
||||||
|
for (uint bar_idx = 0u; bar_idx < config.header0.base_addresses.length; bar_idx++)
|
||||||
|
{
|
||||||
|
/* Read the BAR. */
|
||||||
|
r[0] = config.header0.base_addresses[bar_idx];
|
||||||
|
/* Skip zeroed BARs. */
|
||||||
|
if (r[0] == 0u)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
/* Skip I/O BARs. */
|
||||||
|
if ((r[0] & 1u) != 0u)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
bool is_64bit;
|
||||||
|
uint bar_type = (r[0] >> 1u) & 0x3u;
|
||||||
|
if (bar_type == 0u)
|
||||||
|
{
|
||||||
|
/* 32-bit region. */
|
||||||
|
r[1] = 0u;
|
||||||
|
}
|
||||||
|
else if ((bar_type == 2u) && (bar_idx < 5u))
|
||||||
|
{
|
||||||
|
/* 64-bit region. */
|
||||||
|
is_64bit = true;
|
||||||
|
r[1] = config.header0.base_addresses[bar_idx + 1];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Determine the region length. */
|
||||||
|
config.header0.base_addresses[bar_idx] = 0xFFFF_FFFFu;
|
||||||
|
s[0] = config.header0.base_addresses[bar_idx];
|
||||||
|
config.header0.base_addresses[bar_idx] = r[0];
|
||||||
|
if (is_64bit)
|
||||||
|
{
|
||||||
|
config.header0.base_addresses[bar_idx + 1] = 0xFFFF_FFFFu;
|
||||||
|
s[1] = config.header0.base_addresses[bar_idx + 1];
|
||||||
|
config.header0.base_addresses[bar_idx + 1] = r[1];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
s[1] = 0xFFFF_FFFFu;
|
||||||
|
}
|
||||||
|
ulong mm_region_address = (cast(ulong)r[0] & 0xFFFF_FFF0u) | (cast(ulong)r[1] << 32u);
|
||||||
|
ulong length = ~((cast(ulong)s[0] & 0xFFFF_FFF0u) | (cast(ulong)s[1] << 32u)) + 1u;
|
||||||
|
ulong flags = (r[0] & 0x8) != 0u ? PT_WRITE_THROUGH : 0u;
|
||||||
|
Hurl.identity_map_range(mm_region_address, length, PT_WRITABLE | flags);
|
||||||
|
memory_ranges[range_index].address = mm_region_address;
|
||||||
|
memory_ranges[range_index].length = length;
|
||||||
|
range_index++;
|
||||||
|
if (is_64bit)
|
||||||
|
{
|
||||||
|
bar_idx++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Device list.
|
||||||
|
*/
|
||||||
|
public static __gshared Device * devices;
|
||||||
|
|
||||||
|
private static uint read_config_register(Address address, uint register_id)
|
||||||
|
{
|
||||||
|
uint cfg_addr = 0x8000_0000u |
|
||||||
|
(address.bus_nr << 16u) |
|
||||||
|
(address.device_nr << 11u) |
|
||||||
|
(address.function_nr << 8u) |
|
||||||
|
(register_id << 2u);
|
||||||
|
out32(IO_ADDR_CONFIG_ADDRESS, cfg_addr);
|
||||||
|
return in32(IO_ADDR_CONFIG_DATA);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void write_config_register(Address address, uint register_id, uint value)
|
||||||
|
{
|
||||||
|
uint cfg_addr = 0x8000_0000u |
|
||||||
|
(address.bus_nr << 16u) |
|
||||||
|
(address.device_nr << 11u) |
|
||||||
|
(address.function_nr << 8u) |
|
||||||
|
(register_id << 2u);
|
||||||
|
out32(IO_ADDR_CONFIG_ADDRESS, cfg_addr);
|
||||||
|
out32(IO_ADDR_CONFIG_DATA, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct Configuration
|
||||||
|
{
|
||||||
|
static struct HeaderType0
|
||||||
|
{
|
||||||
|
ushort vendor_id;
|
||||||
|
ushort device_id;
|
||||||
|
ushort command;
|
||||||
|
ushort status;
|
||||||
|
ubyte revision_id;
|
||||||
|
ubyte interface_id;
|
||||||
|
ubyte subclass_id;
|
||||||
|
ubyte class_id;
|
||||||
|
ubyte cache_line_size;
|
||||||
|
ubyte latency_timer;
|
||||||
|
ubyte header_type;
|
||||||
|
ubyte bist;
|
||||||
|
|
||||||
|
uint[6] base_addresses;
|
||||||
|
uint cardbus_cis_pointer;
|
||||||
|
ushort subsystem_vendor_id;
|
||||||
|
ushort subsystem_id;
|
||||||
|
uint expansion_rom_base_address;
|
||||||
|
ubyte capabilities_pointer;
|
||||||
|
ubyte[7] _reserved;
|
||||||
|
ubyte interrupt_line;
|
||||||
|
ubyte interrupt_pin;
|
||||||
|
ubyte min_grant;
|
||||||
|
ubyte max_latency;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* PCI configuration format for header type 1 (PCI-to-PCI bridge).
|
||||||
|
*/
|
||||||
|
static struct HeaderType1
|
||||||
|
{
|
||||||
|
ushort vendor_id;
|
||||||
|
ushort device_id;
|
||||||
|
ushort command;
|
||||||
|
ushort status;
|
||||||
|
ubyte revision_id;
|
||||||
|
ubyte interface_id;
|
||||||
|
ubyte subclass_id;
|
||||||
|
ubyte class_id;
|
||||||
|
ubyte cache_line_size;
|
||||||
|
ubyte latency_timer;
|
||||||
|
ubyte header_type;
|
||||||
|
ubyte bist;
|
||||||
|
|
||||||
|
uint[2] base_addresses;
|
||||||
|
ubyte primary_bus_nr;
|
||||||
|
ubyte secondary_bus_nr;
|
||||||
|
ubyte subordinate_bus_nr;
|
||||||
|
ubyte secondary_latency_timer;
|
||||||
|
ubyte io_base;
|
||||||
|
ubyte io_limit;
|
||||||
|
ushort secondary_status;
|
||||||
|
ushort memory_base;
|
||||||
|
ushort memory_limit;
|
||||||
|
ushort prefetchable_memory_base;
|
||||||
|
ushort prefetchable_memory_limit;
|
||||||
|
uint prefetchable_base_high;
|
||||||
|
uint prefetchable_limit_high;
|
||||||
|
ushort io_base_high;
|
||||||
|
ushort io_limit_high;
|
||||||
|
ubyte capability_pointer;
|
||||||
|
ubyte[3] _reserved;
|
||||||
|
uint expansion_rom_base_address;
|
||||||
|
ubyte interrupt_line;
|
||||||
|
ubyte interrupt_pin;
|
||||||
|
ushort bridge_control;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* PCI configuration format for header type 2 (PCI-to-CardBus bridge).
|
||||||
|
*/
|
||||||
|
static struct HeaderType2
|
||||||
|
{
|
||||||
|
ushort vendor_id;
|
||||||
|
ushort device_id;
|
||||||
|
ushort command;
|
||||||
|
ushort status;
|
||||||
|
ubyte revision_id;
|
||||||
|
ubyte interface_id;
|
||||||
|
ubyte subclass_id;
|
||||||
|
ubyte class_id;
|
||||||
|
ubyte cache_line_size;
|
||||||
|
ubyte latency_timer;
|
||||||
|
ubyte header_type;
|
||||||
|
ubyte bist;
|
||||||
|
|
||||||
|
uint cardbus_base_address;
|
||||||
|
ubyte capability_list_offset;
|
||||||
|
ubyte _reserved;
|
||||||
|
ushort secondary_status;
|
||||||
|
ubyte pci_bus_nr;
|
||||||
|
ubyte cardbus_bus_nr;
|
||||||
|
ubyte subordinate_bus_nr;
|
||||||
|
ubyte cardbus_latency_timer;
|
||||||
|
uint base_address_0;
|
||||||
|
uint memory_limit_0;
|
||||||
|
uint base_address_1;
|
||||||
|
uint memory_limit_1;
|
||||||
|
uint io_base_address_0;
|
||||||
|
uint io_limit_0;
|
||||||
|
uint io_base_address_1;
|
||||||
|
uint io_limit_1;
|
||||||
|
ubyte interrupt_line;
|
||||||
|
ubyte interrupt_pin;
|
||||||
|
ushort bridge_control;
|
||||||
|
ushort subsystem_device_id;
|
||||||
|
ushort subsystem_vendor_id;
|
||||||
|
uint legacy_base_address;
|
||||||
|
}
|
||||||
|
|
||||||
|
union
|
||||||
|
{
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
ushort vendor_id;
|
||||||
|
ushort device_id;
|
||||||
|
ushort command;
|
||||||
|
ushort status;
|
||||||
|
ubyte revision_id;
|
||||||
|
ubyte interface_id;
|
||||||
|
ubyte subclass_id;
|
||||||
|
ubyte class_id;
|
||||||
|
ubyte cache_line_size;
|
||||||
|
ubyte latency_timer;
|
||||||
|
ubyte header_type;
|
||||||
|
ubyte bist;
|
||||||
|
}
|
||||||
|
HeaderType0 header0;
|
||||||
|
HeaderType1 header1;
|
||||||
|
HeaderType2 header2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void scan(ubyte bus_nr, ubyte device_nr, ubyte function_nr, ulong config_address)
|
||||||
|
{
|
||||||
|
Configuration * config = cast(Configuration *)config_address;
|
||||||
|
if (config.vendor_id != 0xFFFFu)
|
||||||
|
{
|
||||||
|
Device * device = A1.allocate!Device();
|
||||||
|
if (devices == null)
|
||||||
|
{
|
||||||
|
devices = device;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
device.list_insert_before(devices);
|
||||||
|
}
|
||||||
|
device.initialize(Address(bus_nr, device_nr, function_nr), config);
|
||||||
|
if (device.multifunction)
|
||||||
|
{
|
||||||
|
for (function_nr = 1u; function_nr < MAX_FUNCTIONS_PER_DEVICE; function_nr++)
|
||||||
|
{
|
||||||
|
config_address += PAGE_SIZE;
|
||||||
|
scan(bus_nr, device_nr, function_nr, config_address);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void initialize()
|
||||||
|
{
|
||||||
|
Klog.writefln("\a3Initializing PCI");
|
||||||
|
MCFG * mcfg = cast(MCFG *)Acpi.get_table("MCFG");
|
||||||
|
size_t n_sgma_descs = mcfg.n_sgma_descs;
|
||||||
|
for (size_t i = 0u; i < n_sgma_descs; i++)
|
||||||
|
{
|
||||||
|
MCFG.SGMADesc * sgma_desc = &mcfg.sgma_descs[i];
|
||||||
|
ulong base_address = sgma_desc.base_address;
|
||||||
|
uint start_pci_bus = sgma_desc.start_pci_bus_number;
|
||||||
|
uint end_pci_bus = sgma_desc.end_pci_bus_number;
|
||||||
|
ulong n_buses = (end_pci_bus - start_pci_bus) + 1u;
|
||||||
|
ulong map_length = PAGE_SIZE * MAX_FUNCTIONS_PER_DEVICE * MAX_DEVICES_PER_BUS * n_buses;
|
||||||
|
Hurl.identity_map_range(base_address, map_length, PT_WRITABLE | PT_DISABLE_CACHE | PT_NO_EXECUTE);
|
||||||
|
for (uint bus_nr = start_pci_bus; bus_nr <= end_pci_bus; bus_nr++)
|
||||||
|
{
|
||||||
|
for (ubyte device_nr = 0u; device_nr < MAX_DEVICES_PER_BUS; device_nr++)
|
||||||
|
{
|
||||||
|
ulong device_address = base_address + ((bus_nr - start_pci_bus) * (MAX_DEVICES_PER_BUS * MAX_FUNCTIONS_PER_DEVICE) + device_nr * MAX_FUNCTIONS_PER_DEVICE) * PAGE_SIZE;
|
||||||
|
scan(cast(ubyte)bus_nr, device_nr, 0u, device_address);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
/**
|
||||||
|
* PIC (Programmable Interrupt Controller) functionality.
|
||||||
|
*/
|
||||||
|
module hulk.pic;
|
||||||
|
|
||||||
|
import hulk.cpu;
|
||||||
|
|
||||||
|
struct Pic
|
||||||
|
{
|
||||||
|
public static void initialize()
|
||||||
|
{
|
||||||
|
/* Disable the PIC. */
|
||||||
|
out8(0xA1u, 0xFFu);
|
||||||
|
out8(0x21u, 0xFFu);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
/**
|
||||||
|
* PIT (Programmable Interval Timer) functionality.
|
||||||
|
*/
|
||||||
|
module hulk.pit;
|
||||||
|
|
||||||
|
import hulk.cpu;
|
||||||
|
import hulk.klog;
|
||||||
|
import hulk.console;
|
||||||
|
import hulk.time;
|
||||||
|
|
||||||
|
struct Pit
|
||||||
|
{
|
||||||
|
/** PIT input frequency in 1/1000 Hz. */
|
||||||
|
private enum ulong PIT_FREQUENCY_1000HZ = 1_193_181_667u;
|
||||||
|
private enum ulong TARGET_INTERRUPT_HZ = 1000u;
|
||||||
|
private enum ulong TARGET_INTERRUPT_1000HZ = TARGET_INTERRUPT_HZ * 1000u;
|
||||||
|
private enum ulong PIT_DIVIDER = (PIT_FREQUENCY_1000HZ + TARGET_INTERRUPT_1000HZ / 2u) / TARGET_INTERRUPT_1000HZ;
|
||||||
|
|
||||||
|
/** PIT channel 0 I/O port. */
|
||||||
|
private enum ubyte PORT_CH_0 = 0x40u;
|
||||||
|
/** PIT channel 1 I/O port. */
|
||||||
|
private enum ubyte PORT_CH_1 = 0x41u;
|
||||||
|
/** PIT channel 2 I/O port. */
|
||||||
|
private enum ubyte PORT_CH_2 = 0x42u;
|
||||||
|
/** PIT mode/command register I/O port. */
|
||||||
|
private enum ubyte PORT_MC = 0x43u;
|
||||||
|
|
||||||
|
private enum ubyte MC_CH_0 = 0x00u;
|
||||||
|
private enum ubyte MC_LO_HI = 0x30u;
|
||||||
|
private enum ubyte MC_RATE_GENERATOR = 0x04u;
|
||||||
|
private enum ubyte MC_BINARY = 0x00u;
|
||||||
|
|
||||||
|
public static void initialize()
|
||||||
|
{
|
||||||
|
Klog.writefln("\a3Initializing PIT");
|
||||||
|
out8(PORT_MC, MC_CH_0 | MC_LO_HI | MC_RATE_GENERATOR | MC_BINARY);
|
||||||
|
out8(PORT_CH_0, PIT_DIVIDER & 0xFFu);
|
||||||
|
out8(PORT_CH_0, PIT_DIVIDER >> 8u);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void isr()
|
||||||
|
{
|
||||||
|
Time.ms_isr();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
module hulk.range;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Representation of a memory range.
|
||||||
|
*/
|
||||||
|
struct Range
|
||||||
|
{
|
||||||
|
/** Range base address. */
|
||||||
|
void * _address;
|
||||||
|
|
||||||
|
/** Range length. */
|
||||||
|
size_t length;
|
||||||
|
|
||||||
|
/** Set the range address. */
|
||||||
|
@property void * address(T)(T a)
|
||||||
|
{
|
||||||
|
static assert(T.sizeof == size_t.sizeof);
|
||||||
|
_address = cast(void *)a;
|
||||||
|
return _address;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Get the range address. */
|
||||||
|
@property void * address()
|
||||||
|
{
|
||||||
|
return _address;
|
||||||
|
}
|
||||||
|
}
|
||||||
+150
@@ -0,0 +1,150 @@
|
|||||||
|
/**
|
||||||
|
* RTC (Real-Time Clock) functionality.
|
||||||
|
*/
|
||||||
|
module hulk.rtc;
|
||||||
|
|
||||||
|
import hulk.cpu;
|
||||||
|
import hulk.klog;
|
||||||
|
import hulk.console;
|
||||||
|
|
||||||
|
struct Rtc
|
||||||
|
{
|
||||||
|
private enum ubyte PORT_SELECT = 0x70u;
|
||||||
|
private enum ubyte PORT_DATA = 0x71u;
|
||||||
|
|
||||||
|
private enum ubyte DISABLE_NMI = 0x80u;
|
||||||
|
|
||||||
|
private enum ubyte REG_RTC_SECONDS = 0x0u;
|
||||||
|
private enum ubyte REG_RTC_MINUTES = 0x2u;
|
||||||
|
private enum ubyte REG_RTC_HOURS = 0x4u;
|
||||||
|
private enum ubyte REG_RTC_DAY = 0x7u;
|
||||||
|
private enum ubyte REG_RTC_MONTH = 0x8u;
|
||||||
|
private enum ubyte REG_RTC_YEAR = 0x9u;
|
||||||
|
private enum ubyte REG_SR_A = 0xAu;
|
||||||
|
private enum ubyte REG_SR_B = 0xBu;
|
||||||
|
private enum ubyte REG_SR_C = 0xCu;
|
||||||
|
|
||||||
|
private enum ubyte SR_A_RTC_UPDATE_IN_PROGRESS = 0x80u;
|
||||||
|
private enum ubyte SR_B_24HOUR = 0x02u;
|
||||||
|
private enum ubyte SR_B_BINARY = 0x04u;
|
||||||
|
private enum ubyte SR_B_ENABLE_IRQ = 0x40u;
|
||||||
|
|
||||||
|
private static __gshared bool rtc_24hour_mode;
|
||||||
|
private static __gshared bool rtc_binary_mode;
|
||||||
|
|
||||||
|
static struct time
|
||||||
|
{
|
||||||
|
public ubyte year;
|
||||||
|
public ubyte month;
|
||||||
|
public ubyte day;
|
||||||
|
public ubyte hour;
|
||||||
|
public ubyte minute;
|
||||||
|
public ubyte second;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void initialize()
|
||||||
|
{
|
||||||
|
Klog.writefln("\a3Initializing RTC");
|
||||||
|
|
||||||
|
/* Enable IRQ 8 to receive RTC interrupts. */
|
||||||
|
ubyte sr_b = read_register(REG_SR_B, true);
|
||||||
|
write_register(REG_SR_B, sr_b | SR_B_ENABLE_IRQ, true);
|
||||||
|
|
||||||
|
/* Record RTC mode flags from status register B. */
|
||||||
|
rtc_24hour_mode = (sr_b & SR_B_24HOUR) != 0u;
|
||||||
|
rtc_binary_mode = (sr_b & SR_B_BINARY) != 0u;
|
||||||
|
|
||||||
|
/* Send EOI to enable more RTC interrupts and re-enable NMIs. */
|
||||||
|
eoi();
|
||||||
|
|
||||||
|
time t = Rtc.read_rtc_time();
|
||||||
|
Klog.writefln("System time is 20%02u-%02u-%02u %02u:%02u:%02u",
|
||||||
|
t.year, t.month, t.day, t.hour, t.minute, t.second);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ubyte read_register(ubyte register, bool disable_nmi = false)
|
||||||
|
{
|
||||||
|
out8(PORT_SELECT, register | (disable_nmi ? DISABLE_NMI : 0u));
|
||||||
|
return in8(PORT_DATA);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void write_register(ubyte register, ubyte value, bool disable_nmi = false)
|
||||||
|
{
|
||||||
|
out8(PORT_SELECT, register | (disable_nmi ? DISABLE_NMI : 0u));
|
||||||
|
out8(PORT_DATA, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void eoi()
|
||||||
|
{
|
||||||
|
/* Read from status register C to clear the interrupt. */
|
||||||
|
read_register(REG_SR_C);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void isr()
|
||||||
|
{
|
||||||
|
static __gshared ulong count;
|
||||||
|
count++;
|
||||||
|
if ((count % 1024) == 0u)
|
||||||
|
{
|
||||||
|
Console.update_header();
|
||||||
|
}
|
||||||
|
eoi();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static time read_rtc_time()
|
||||||
|
{
|
||||||
|
time[2] times;
|
||||||
|
size_t reads;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
times[reads & 1] = read_rtc_time_unsafe();
|
||||||
|
reads++;
|
||||||
|
} while ((reads < 2u) || (times[0] != times[1]));
|
||||||
|
return times[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
private static time read_rtc_time_unsafe()
|
||||||
|
{
|
||||||
|
time t;
|
||||||
|
while ((read_register(REG_SR_A) & SR_A_RTC_UPDATE_IN_PROGRESS) != 0u)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
t.year = read_rtc_time_register(REG_RTC_YEAR);
|
||||||
|
t.month = read_rtc_time_register(REG_RTC_MONTH);
|
||||||
|
t.day = read_rtc_time_register(REG_RTC_DAY);
|
||||||
|
t.minute = read_rtc_time_register(REG_RTC_MINUTES);
|
||||||
|
t.second = read_rtc_time_register(REG_RTC_SECONDS);
|
||||||
|
/* Hour field is treated differently because it might have 'PM' flag
|
||||||
|
* in highest bit if we're in 24-hour mode. */
|
||||||
|
ubyte hour = read_register(REG_RTC_HOURS);
|
||||||
|
ubyte pm = hour >> 7u;
|
||||||
|
hour &= 0x7Fu;
|
||||||
|
if (!rtc_binary_mode)
|
||||||
|
{
|
||||||
|
/* Hour is in BCD format; convert to binary. */
|
||||||
|
hour = bcd_to_binary(hour);
|
||||||
|
}
|
||||||
|
if (!rtc_24hour_mode)
|
||||||
|
{
|
||||||
|
/* Hour is in 12-hour format; convert to 24-hour format. */
|
||||||
|
hour = cast(ubyte)((hour % 12u) + pm * 12u);
|
||||||
|
}
|
||||||
|
t.hour = hour;
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ubyte read_rtc_time_register(ubyte register)
|
||||||
|
{
|
||||||
|
ubyte value = read_register(register);
|
||||||
|
if (!rtc_binary_mode)
|
||||||
|
{
|
||||||
|
value = bcd_to_binary(value);
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ubyte bcd_to_binary(ubyte bcd)
|
||||||
|
{
|
||||||
|
return (bcd & 0xFu) + ((bcd & 0xF0u) >> 4u) * 10u;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
/**
|
||||||
|
* Serial port access.
|
||||||
|
*/
|
||||||
|
module hulk.serial;
|
||||||
|
|
||||||
|
import hulk.cpu;
|
||||||
|
import core.stdc.stdarg;
|
||||||
|
static import hulk.writef;
|
||||||
|
|
||||||
|
struct Serial
|
||||||
|
{
|
||||||
|
enum BASE_PORT = 0x3F8;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize the serial module.
|
||||||
|
*/
|
||||||
|
static void initialize()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write a character to the serial port.
|
||||||
|
*
|
||||||
|
* @param ch Character to write.
|
||||||
|
*/
|
||||||
|
static void write(ubyte ch)
|
||||||
|
{
|
||||||
|
if (ch == '\n')
|
||||||
|
{
|
||||||
|
out8(BASE_PORT, '\r');
|
||||||
|
}
|
||||||
|
out8(BASE_PORT, ch);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write a formatted string to the serial port.
|
||||||
|
*
|
||||||
|
* @param s Format string.
|
||||||
|
* @param args Variable arguments structure.
|
||||||
|
*/
|
||||||
|
public static void writef(string s, va_list args)
|
||||||
|
{
|
||||||
|
hulk.writef.writefv(function void(ubyte ch) {
|
||||||
|
Serial.write(ch);
|
||||||
|
}, s, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write a formatted string to the serial port.
|
||||||
|
*
|
||||||
|
* @param s Format string.
|
||||||
|
*/
|
||||||
|
public static extern (C) void writef(string s, ...)
|
||||||
|
{
|
||||||
|
va_list args;
|
||||||
|
va_start(args, s);
|
||||||
|
writef(s, args);
|
||||||
|
va_end(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write a formatted string and newline to the serial port.
|
||||||
|
*
|
||||||
|
* @param s Format string.
|
||||||
|
*/
|
||||||
|
public static extern (C) void writefln(string s, ...)
|
||||||
|
{
|
||||||
|
va_list args;
|
||||||
|
va_start(args, s);
|
||||||
|
writef(s, args);
|
||||||
|
writef("\n", args);
|
||||||
|
va_end(args);
|
||||||
|
}
|
||||||
|
}
|
||||||
+139
@@ -0,0 +1,139 @@
|
|||||||
|
/**
|
||||||
|
* Kernel tests.
|
||||||
|
*/
|
||||||
|
module hulk.test;
|
||||||
|
|
||||||
|
import hulk.klog;
|
||||||
|
import hulk.list;
|
||||||
|
import hulk.time;
|
||||||
|
import hulk.hurl.a1;
|
||||||
|
import core.volatile;
|
||||||
|
|
||||||
|
struct Test
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run tests.
|
||||||
|
*/
|
||||||
|
public static void run()
|
||||||
|
{
|
||||||
|
Klog.writefln("\a3Running kernel tests");
|
||||||
|
test_pit();
|
||||||
|
test_list();
|
||||||
|
Klog.writefln("\a3Kernel tests complete");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void test_pit()
|
||||||
|
{
|
||||||
|
Klog.writefln("Testing PIT...");
|
||||||
|
/* Check that PIT millisecond interrupt is firing. */
|
||||||
|
size_t uptime = Time.uptime();
|
||||||
|
while (Time.uptime() <= uptime)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void test_list()
|
||||||
|
{
|
||||||
|
Klog.writefln("Testing list...");
|
||||||
|
|
||||||
|
struct S
|
||||||
|
{
|
||||||
|
size_t v;
|
||||||
|
mixin List!S;
|
||||||
|
void initialize()
|
||||||
|
{
|
||||||
|
list_init();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
S * s1 = A1.allocate!S();
|
||||||
|
s1.initialize();
|
||||||
|
s1.v = 33;
|
||||||
|
assert_eq(1, s1.list_count);
|
||||||
|
S * s2 = A1.allocate!S();
|
||||||
|
s2.initialize();
|
||||||
|
s2.v = 42;
|
||||||
|
s2.list_insert_after(s1);
|
||||||
|
S * s3 = A1.allocate!S();
|
||||||
|
s3.initialize();
|
||||||
|
s3.v = 55;
|
||||||
|
s3.list_insert_before(s1);
|
||||||
|
size_t count;
|
||||||
|
assert_eq(3, s1.list_count);
|
||||||
|
assert_eq(3, s2.list_count);
|
||||||
|
assert_eq(3, s3.list_count);
|
||||||
|
foreach (entry; *s1)
|
||||||
|
{
|
||||||
|
switch (count)
|
||||||
|
{
|
||||||
|
case 0:
|
||||||
|
assert_eq(33, entry.v);
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
assert_eq(42, entry.v);
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
assert_eq(55, entry.v);
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
assert_eq(0, 1);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
foreach (entry; *s3)
|
||||||
|
{
|
||||||
|
if (entry.v == 33)
|
||||||
|
{
|
||||||
|
entry.list_remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert_eq(2, s3.list_count);
|
||||||
|
count = 0;
|
||||||
|
foreach (entry; *s3)
|
||||||
|
{
|
||||||
|
switch (count)
|
||||||
|
{
|
||||||
|
case 0:
|
||||||
|
assert_eq(55, entry.v);
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
assert_eq(42, entry.v);
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
assert_eq(0, 1);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
foreach (entry; *s2)
|
||||||
|
{
|
||||||
|
if (entry.v == 42)
|
||||||
|
{
|
||||||
|
entry.list_remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert_eq(1, s2.list_count);
|
||||||
|
assert_eq(1, s3.list_count);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void assert_eq(T)(T first, T second, size_t line_number = __LINE__)
|
||||||
|
{
|
||||||
|
if (first != second)
|
||||||
|
{
|
||||||
|
Klog.fatal_error("Assertion failed! %x != %x (line %u)", first, second, line_number);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void assert_neq(T)(T first, T second, size_t line_number = __LINE__)
|
||||||
|
{
|
||||||
|
if (first == second)
|
||||||
|
{
|
||||||
|
Klog.fatal_error("Assertion failed! %x == %x (line %u)", first, second, line_number);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,143 @@
|
|||||||
|
/**
|
||||||
|
* Kernel thread functionality.
|
||||||
|
*/
|
||||||
|
module hulk.thread;
|
||||||
|
|
||||||
|
import hulk.hurl.a1;
|
||||||
|
import hulk.him;
|
||||||
|
import hulk.gdt;
|
||||||
|
import hulk.memory;
|
||||||
|
import hulk.list;
|
||||||
|
import hulk.cpu;
|
||||||
|
import hulk.hurl;
|
||||||
|
import ldc.llvmasm;
|
||||||
|
|
||||||
|
struct Thread
|
||||||
|
{
|
||||||
|
/* TODO: currently kernel thread stack size is fixed. */
|
||||||
|
enum STACK_SIZE = 16 * 1024;
|
||||||
|
|
||||||
|
enum ulong INT_TCTRL_YIELD = 0;
|
||||||
|
|
||||||
|
/** Interrupt stack template. */
|
||||||
|
static immutable __gshared ulong[ISF_COUNT] interrupt_stack_template = [
|
||||||
|
0x16161616_16161616u, /* RBP */
|
||||||
|
0x15151515_15151515u, /* R15 */
|
||||||
|
0x14141414_14141414u, /* R14 */
|
||||||
|
0x13131313_13131313u, /* R13 */
|
||||||
|
0x12121212_12121212u, /* R12 */
|
||||||
|
0x11111111_11111111u, /* R11 */
|
||||||
|
0x10101010_10101010u, /* R10 */
|
||||||
|
0x09090909_09090909u, /* R9 */
|
||||||
|
0x08080808_08080808u, /* R8 */
|
||||||
|
0x06060606_06060606u, /* RDI */
|
||||||
|
0x05050505_05050505u, /* RSI */
|
||||||
|
0x04040404_04040404u, /* RDX */
|
||||||
|
0x03030303_03030303u, /* RCX */
|
||||||
|
0x02020202_02020202u, /* RBX */
|
||||||
|
0x01010101_01010101u, /* RAX */
|
||||||
|
0u, /* Error Code */
|
||||||
|
0u, /* RIP */
|
||||||
|
Gdt.SELECTOR_KERNEL_CODE, /* CS */
|
||||||
|
0u, /* RFLAGS */
|
||||||
|
0u, /* RSP */
|
||||||
|
Gdt.SELECTOR_KERNEL_DATA, /* SS */
|
||||||
|
];
|
||||||
|
|
||||||
|
/** List of all runnable kernel threads. */
|
||||||
|
private static __gshared Thread * runnable_threads;
|
||||||
|
|
||||||
|
/** Currently executing kernel thread. */
|
||||||
|
static __gshared Thread * current_thread;
|
||||||
|
|
||||||
|
/** Current kernel thread stack pointer. */
|
||||||
|
ulong * stack_pointer;
|
||||||
|
static assert(stack_pointer.offsetof == 0);
|
||||||
|
|
||||||
|
/** Base address of kernel thread stack. */
|
||||||
|
void * stack_addr;
|
||||||
|
|
||||||
|
mixin List!Thread;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize a kernel thread.
|
||||||
|
*/
|
||||||
|
public void initialize(void function() fn)
|
||||||
|
{
|
||||||
|
list_init();
|
||||||
|
suspend_interrupts();
|
||||||
|
list_insert_after(current_thread);
|
||||||
|
resume_interrupts();
|
||||||
|
stack_addr = A1.allocate(STACK_SIZE);
|
||||||
|
ulong * stack_top = cast(ulong *)(stack_addr + STACK_SIZE);
|
||||||
|
stack_pointer = stack_top - ISF_COUNT;
|
||||||
|
memcpy64(stack_pointer, &interrupt_stack_template[0], ISF_COUNT);
|
||||||
|
stack_pointer[ISF_RIP] = cast(ulong)fn;
|
||||||
|
stack_pointer[ISF_RFLAGS] = read_rflags();
|
||||||
|
stack_pointer[ISF_RSP] = cast(ulong)stack_top;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize first kernel thread.
|
||||||
|
*/
|
||||||
|
public void initialize_first_thread()
|
||||||
|
{
|
||||||
|
list_init();
|
||||||
|
runnable_threads = &this;
|
||||||
|
stack_addr = cast(void *)(Hurl.HULK_STACK_TOP - STACK_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize kernel threading.
|
||||||
|
*/
|
||||||
|
public static void initialize()
|
||||||
|
{
|
||||||
|
Thread * thread = A1.allocate!Thread();
|
||||||
|
thread.initialize_first_thread();
|
||||||
|
current_thread = thread;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create and start a kernel thread.
|
||||||
|
*
|
||||||
|
* TODO: fn must not return.
|
||||||
|
* TODO: threads are always runnable.
|
||||||
|
* TODO: threads cannot be destroyed.
|
||||||
|
*/
|
||||||
|
public static Thread * start(void function() fn)
|
||||||
|
{
|
||||||
|
Thread * thread = A1.allocate!Thread();
|
||||||
|
thread.initialize(fn);
|
||||||
|
swint(INT_TCTRL_YIELD);
|
||||||
|
return thread;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Trigger a thread management software interrupt.
|
||||||
|
*/
|
||||||
|
public static void swint(T1)(T1 v1)
|
||||||
|
{
|
||||||
|
mixin(`__asm("int $$`, cast(int)INT_KERNEL_SWINT, `", "{rax}", v1);`);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle a thread management software interrupt.
|
||||||
|
*
|
||||||
|
* This function is called in interrupt context.
|
||||||
|
*
|
||||||
|
* @param stack_frame
|
||||||
|
* Pointer to the stack frame.
|
||||||
|
*/
|
||||||
|
public static void handle_swint(ulong * stack_frame)
|
||||||
|
{
|
||||||
|
switch (stack_frame[ISF_RAX])
|
||||||
|
{
|
||||||
|
case INT_TCTRL_YIELD:
|
||||||
|
Thread.current_thread = Thread.current_thread.list_next;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
/**
|
||||||
|
* Time functionality.
|
||||||
|
*/
|
||||||
|
module hulk.time;
|
||||||
|
|
||||||
|
import hulk.volatile;
|
||||||
|
|
||||||
|
struct Time
|
||||||
|
{
|
||||||
|
/** System uptime (ms). */
|
||||||
|
private static __gshared Volatile!ulong s_uptime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Millisecond ISR.
|
||||||
|
*/
|
||||||
|
public static void ms_isr()
|
||||||
|
{
|
||||||
|
s_uptime++;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get system uptime (ms).
|
||||||
|
*/
|
||||||
|
public static @property ulong uptime()
|
||||||
|
{
|
||||||
|
return s_uptime;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sleep for the given amount of time (ms).
|
||||||
|
*
|
||||||
|
* @param count
|
||||||
|
* Number of milliseconds to sleep for.
|
||||||
|
*/
|
||||||
|
public static void msleep(ulong count)
|
||||||
|
{
|
||||||
|
ulong wait_for = uptime() + count + 1;
|
||||||
|
while (uptime() < wait_for)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
module hulk.usb;
|
||||||
|
|
||||||
|
public import hulk.usb.usb;
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
/**
|
||||||
|
* USB (Universal Serial Bus) functionality.
|
||||||
|
*/
|
||||||
|
module hulk.usb.usb;
|
||||||
|
|
||||||
|
import hulk.klog;
|
||||||
|
import hulk.usb.xhci;
|
||||||
|
import hulk.pci;
|
||||||
|
|
||||||
|
struct Usb
|
||||||
|
{
|
||||||
|
public static void initialize()
|
||||||
|
{
|
||||||
|
Klog.writefln("\a3Initializing USB");
|
||||||
|
|
||||||
|
foreach (pci_device; *Pci.devices)
|
||||||
|
{
|
||||||
|
if (pci_device.type == Pci.XHCI_CONTROLLER)
|
||||||
|
{
|
||||||
|
XHCI.build(pci_device);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,535 @@
|
|||||||
|
/**
|
||||||
|
* eXtensible Host Controller Interface for Universal Serial Bus
|
||||||
|
*
|
||||||
|
* Documentation:
|
||||||
|
* - https://wiki.osdev.org/EXtensible_Host_Controller_Interface
|
||||||
|
* - https://www.intel.com/content/dam/www/public/us/en/documents/technical-specifications/extensible-host-controler-interface-usb-xhci.pdf
|
||||||
|
*/
|
||||||
|
|
||||||
|
module hulk.usb.xhci;
|
||||||
|
|
||||||
|
import hulk.pci;
|
||||||
|
import hulk.hurl;
|
||||||
|
import hulk.hurl.a1;
|
||||||
|
import hulk.hippo;
|
||||||
|
import hulk.klog;
|
||||||
|
import hulk.time;
|
||||||
|
import hulk.volatile;
|
||||||
|
|
||||||
|
struct XHCI
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Located at PCI base address.
|
||||||
|
*
|
||||||
|
* All are read-only.
|
||||||
|
*/
|
||||||
|
struct CapabilityRegisters
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Offset from beginning of capability registers to beginning of
|
||||||
|
* operational registers.
|
||||||
|
*/
|
||||||
|
Volatile!ubyte capability_length;
|
||||||
|
|
||||||
|
ubyte _reserved01;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface version number supported (psuedo-BSD).
|
||||||
|
*/
|
||||||
|
Volatile!ushort hci_version;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* HCSPARAMS1.
|
||||||
|
* 7:0 number of device slots (MaxSlots)
|
||||||
|
* 18:8 number of interrupters (MaxIntrs)
|
||||||
|
* 23:19 reserved
|
||||||
|
* 31:24 number of ports (MaxPorts)
|
||||||
|
*/
|
||||||
|
Volatile!uint hcs_params1;
|
||||||
|
|
||||||
|
@property ubyte max_slots()
|
||||||
|
{
|
||||||
|
return this.hcs_params1 & 0xFFu;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 3:0 isochronous scheduling threshold (IST)
|
||||||
|
* 7:4 event ring segment table max (ERST Max)
|
||||||
|
* 20:8 reserved
|
||||||
|
* 25:21 max scratchpad buffers (high 5 bits; xHCI v1.1+)
|
||||||
|
* 26 scratchpad restore (SPR)
|
||||||
|
* 31:27 max scratchpad buffers (low 5 bits)
|
||||||
|
*/
|
||||||
|
Volatile!uint hcs_params2;
|
||||||
|
|
||||||
|
@property size_t max_scratchpad_buffers()
|
||||||
|
{
|
||||||
|
uint v = this.hcs_params2;
|
||||||
|
return (v >> 27u) | ((v >> (21 - 5)) & 0x3E0u);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 7:0 U1 device exit latency
|
||||||
|
* 15:8 reserved
|
||||||
|
* 31:16 U2 device exit latency
|
||||||
|
*/
|
||||||
|
Volatile!uint hcs_params3;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 0 64-bit addressing capability (AC64)
|
||||||
|
* 1 BW negotiation capability (BNC)
|
||||||
|
* 2 context size (CSZ)
|
||||||
|
* 3 port power control (PPC)
|
||||||
|
* 4 port indicators (PIND)
|
||||||
|
* 5 light HC reset capability (LHRC)
|
||||||
|
* 6 latency tolerance messaging capability (LTC)
|
||||||
|
* 7 no secondary SID support (NSS)
|
||||||
|
* 8 parse all event data (PAE; xHCI v1.1+)
|
||||||
|
* 9 stopped - short packet capability (SPC; xHCI v1.1+)
|
||||||
|
* 10 stopped EDTLA capability (SEC; xHCI v1.1+)
|
||||||
|
* 11 continuous frame ID capability (CFC; xHCI v1.1+)
|
||||||
|
* 15:12 maximum primary stream array size (MaxPSASize)
|
||||||
|
* 31:16 xHCI extended capabilities pointer (xECP)
|
||||||
|
* offset in 32-bit words from base of capability registers to base
|
||||||
|
* of capabilities list
|
||||||
|
*/
|
||||||
|
Volatile!uint hcc_params1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 1:0 reserved
|
||||||
|
* 31:2 doorbell array offset in 32-bit words
|
||||||
|
* offset in 32-bit words from base of capability registers to the
|
||||||
|
* doorbell array
|
||||||
|
*/
|
||||||
|
Volatile!uint doorbell_offset;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 4:0 reserved
|
||||||
|
* 31:5 runtime register space offset
|
||||||
|
*/
|
||||||
|
Volatile!uint rts_offset;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 0 U3 entry capability (U3C)
|
||||||
|
* 1 ConfigEP command max exit latency too large (CMC)
|
||||||
|
* 2 force save context capability (FSC)
|
||||||
|
* 3 compliance transition capability (CTC)
|
||||||
|
* 4 large ESIT payload capability (LEC)
|
||||||
|
* 5 configuration information capability (CIC)
|
||||||
|
* 6 extended TBC capability (ETC)
|
||||||
|
* 7 extended TBC TRB status capability (ETC_TSC)
|
||||||
|
* 8 get/set extended property capability (GSC; xHCI v1.1+)
|
||||||
|
* 9 virtualization based trusted I/O capability (VTC; xHCI v1.2+)
|
||||||
|
* 31:10 reserved
|
||||||
|
*/
|
||||||
|
Volatile!uint hcc_params2;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 11:0 reserved
|
||||||
|
* 31:12 VTIO register space offset (xHCI v1.2+)
|
||||||
|
*/
|
||||||
|
Volatile!uint vtios_offset;
|
||||||
|
}
|
||||||
|
static assert(CapabilityRegisters.sizeof == 0x24u);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Located at offset capability_length from PCI base address.
|
||||||
|
*/
|
||||||
|
struct OperationalRegisters
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 0 run/stop (RS)
|
||||||
|
* 1 host controller reset (HCRST)
|
||||||
|
* 2 interrupter enable (INTE)
|
||||||
|
* 3 host system error enable (HSEE)
|
||||||
|
* 6:4 reserved
|
||||||
|
* 7 light host controller reset (LHCRST)
|
||||||
|
* 8 controller save state (CSS)
|
||||||
|
* 9 controller restore state (CRS)
|
||||||
|
* 10 enable wrap event (EWE)
|
||||||
|
* 11 enable U3 MFINDEX stop (UE3S)
|
||||||
|
* 12 stopped short packet enable (SPE; xHCI v1.1+)
|
||||||
|
* 13 CEM enable (CME; xHCI v1.1+)
|
||||||
|
* 14 extended TBC enable (ETE; xHCI v1.2+)
|
||||||
|
* 15 extended TBC TRB status enable (TSC_EN; xHCI v1.2+)
|
||||||
|
* 16 VTIO enable (VTIOE; xHCI v1.2+)
|
||||||
|
* 31:17 reserved
|
||||||
|
*/
|
||||||
|
Volatile!uint usb_command;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 0 HCHalted
|
||||||
|
* 1 reserved
|
||||||
|
* 2 Host System Error (HSE)
|
||||||
|
* 3 Event Interrupt (EINT)
|
||||||
|
* 4 Port Change Detected (PCD)
|
||||||
|
* 7:5 reserved
|
||||||
|
* 8 Save State Status (SSS)
|
||||||
|
* 9 Restore State Status (RSS)
|
||||||
|
* 10 Save/Restore Error (SRE)
|
||||||
|
* 11 Controller Not Ready (CNR)
|
||||||
|
* 12 Host Controller Error (HCE)
|
||||||
|
* 31:13 reserved
|
||||||
|
*/
|
||||||
|
Volatile!uint usb_status;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 15:0 Page Size (actual page size is 2^(x+12))
|
||||||
|
* 31:16 reserved
|
||||||
|
*/
|
||||||
|
Volatile!uint page_size;
|
||||||
|
|
||||||
|
uint[2] _reserved01;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 15:0 Notification Enable
|
||||||
|
* 31:16 reserved
|
||||||
|
*/
|
||||||
|
Volatile!uint dn_ctrl;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 0 Ring Cycle State (RCS)
|
||||||
|
* 1 Command Stop (CS)
|
||||||
|
* 2 Command Abort (CA)
|
||||||
|
* 3 Command Ring Running (CRR)
|
||||||
|
* 5:4 reserved
|
||||||
|
* 63:6 Command Ring Pointer
|
||||||
|
*/
|
||||||
|
Volatile!ulong cr_ctrl;
|
||||||
|
|
||||||
|
uint[4] _reserved02;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 5:0 reserved
|
||||||
|
* 63:6 Device Context Base Address Array Pointer
|
||||||
|
*/
|
||||||
|
Volatile!ulong dcbaap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 7:0 Max Device Slots Enabled (MaxSlotsEn)
|
||||||
|
* 8 U3 Entry Enable (U3E; xHCI v1.1+)
|
||||||
|
* 9 Configuration Information Enable (CIE; xHCI v1.1+)
|
||||||
|
* 31:10 reserved
|
||||||
|
*/
|
||||||
|
Volatile!uint config;
|
||||||
|
|
||||||
|
uint _reserved03;
|
||||||
|
}
|
||||||
|
static assert(OperationalRegisters.sizeof == 0x40u);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Located at offset 0x400 from start of Operational Registers.
|
||||||
|
*
|
||||||
|
* There is one set of these registers for each port of the root hub.
|
||||||
|
*/
|
||||||
|
struct PortRegisters
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Port Status and Control
|
||||||
|
*
|
||||||
|
* 0 Current Connect Status
|
||||||
|
* 1 Port Enabled/Disabled
|
||||||
|
* 2 reserved
|
||||||
|
* 3 Over-current active
|
||||||
|
* 4 Port Reset
|
||||||
|
* 8:5 Port Link State
|
||||||
|
* 9 Port Power
|
||||||
|
* 13:10 Port Speed
|
||||||
|
* 15:14 Port Indicator Control
|
||||||
|
* 16 Port Link State Write Strobe
|
||||||
|
* 17 Connect Status Change
|
||||||
|
* 18 Port Enable/Disable Change
|
||||||
|
* 19 Warm Port Reset Change
|
||||||
|
* 20 Over-current Change
|
||||||
|
* 21 Port Reset Change
|
||||||
|
* 22 Port Link State Change
|
||||||
|
* 23 Port Config Error Change
|
||||||
|
* 24 Cold Attach Status
|
||||||
|
* 25 Wake on Connect Enable
|
||||||
|
* 26 Wake on Disconnect Enable
|
||||||
|
* 27 Wake on Over-current Enable
|
||||||
|
* 29:28 reserved
|
||||||
|
* 30 Device Removable
|
||||||
|
* 31 Warm Port Reset
|
||||||
|
*/
|
||||||
|
Volatile!uint portsc;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Port Power Management Status and Control
|
||||||
|
*/
|
||||||
|
Volatile!uint portpmsc;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Port Link Info
|
||||||
|
*/
|
||||||
|
Volatile!uint portli;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Port Hardware LPM Control (xHCI v1.1+)
|
||||||
|
*/
|
||||||
|
Volatile!uint porthlpmc;
|
||||||
|
}
|
||||||
|
static assert(PortRegisters.sizeof == 0x10u);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Located at offset rts_offset from PCI base address.
|
||||||
|
*/
|
||||||
|
struct RuntimeRegisters
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Microframe index. Incremented by controller each microframe.
|
||||||
|
*/
|
||||||
|
Volatile!uint mfindex;
|
||||||
|
|
||||||
|
uint[7] _reserved01;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interrupter register sets.
|
||||||
|
*/
|
||||||
|
InterrupterRegister[0] ir;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interrupter register set.
|
||||||
|
*
|
||||||
|
* A copy of this register set is present for each interrupter.
|
||||||
|
*/
|
||||||
|
struct InterrupterRegister
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Interrupter Management Register.
|
||||||
|
* 0 Interrupt Pending (IP)
|
||||||
|
* 1 Interrupt Enable
|
||||||
|
* 31:2 reserved
|
||||||
|
*/
|
||||||
|
Volatile!uint imr;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interrupter Moderation.
|
||||||
|
* 15:0 Interrupt Moderation Interval
|
||||||
|
* 31:16 Interrupt Moderation Counter
|
||||||
|
*/
|
||||||
|
Volatile!uint im;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Event Ring Segment Table Size.
|
||||||
|
* 15:0 Event Ring Segment Table Size
|
||||||
|
* 31:16 reserved
|
||||||
|
*/
|
||||||
|
Volatile!uint ersts;
|
||||||
|
|
||||||
|
uint reserved;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Event Ring Segment Table Base Address.
|
||||||
|
* 5:0 reserved
|
||||||
|
* 63:6 Event Ring Segment Table Base Address
|
||||||
|
*/
|
||||||
|
Volatile!ulong erstba;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Event Ring Dequeue Pointer.
|
||||||
|
* 2:0 Dequeue ERST Segment Index
|
||||||
|
* 3 Event Handler Busy
|
||||||
|
* 63:4 Event Ring Dequeue Pointer
|
||||||
|
*/
|
||||||
|
Volatile!ulong erdp;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Located at offset doorbell_offset from PCI base address.
|
||||||
|
*
|
||||||
|
* The number of these is given by HCSPARAMS1.MaxSlots + 1 (for the command
|
||||||
|
* ring).
|
||||||
|
*/
|
||||||
|
struct DoorbellRegister
|
||||||
|
{
|
||||||
|
Volatile!ubyte target;
|
||||||
|
ubyte _reserved01;
|
||||||
|
Volatile!ushort task_id;
|
||||||
|
}
|
||||||
|
static assert(DoorbellRegister.sizeof == 4u);
|
||||||
|
|
||||||
|
private CapabilityRegisters * m_capability_registers;
|
||||||
|
private OperationalRegisters * m_operational_registers;
|
||||||
|
private PortRegisters * m_port_registers;
|
||||||
|
private RuntimeRegisters * m_runtime_registers;
|
||||||
|
private DoorbellRegister * m_doorbell_registers;
|
||||||
|
|
||||||
|
void initialize(Pci.Device * pci_device)
|
||||||
|
{
|
||||||
|
void * base_address = pci_device.memory_ranges[0].address;
|
||||||
|
Klog.writefln("Found XHCI controller at address %p", base_address);
|
||||||
|
m_capability_registers = cast(CapabilityRegisters *)base_address;
|
||||||
|
m_operational_registers = cast(OperationalRegisters *)(base_address + m_capability_registers.capability_length);
|
||||||
|
m_port_registers = cast(PortRegisters *)(cast(void *)m_operational_registers + 0x400);
|
||||||
|
m_runtime_registers = cast(RuntimeRegisters *)(base_address + m_capability_registers.rts_offset);
|
||||||
|
m_doorbell_registers = cast(DoorbellRegister *)(base_address + m_capability_registers.doorbell_offset);
|
||||||
|
dump_extended_capabilities();
|
||||||
|
if (!reset())
|
||||||
|
{
|
||||||
|
Klog.writefln("XHCI controller failed to initialize");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Allocate memory for various controller data structures. */
|
||||||
|
ubyte * page = cast(ubyte *)Hippo.allocate_page();
|
||||||
|
/* Device Context Index: 2K */
|
||||||
|
void ** device_context_index = cast(void **)page;
|
||||||
|
/* Input Control Context: 64 bytes */
|
||||||
|
void * input_control_context = &page[2048];
|
||||||
|
/* Slot Context: 64 bytes */
|
||||||
|
void * slot_context = &page[2048 + 64];
|
||||||
|
/* Endpoint Context: 64 bytes */
|
||||||
|
void * endpoint_context = &page[2048 + 64 + 64];
|
||||||
|
/* Stream Context: 16 bytes */
|
||||||
|
void * stream_context = &page[2048 + 64 + 64 + 64];
|
||||||
|
/* Stream Array (Linear): 1M */
|
||||||
|
void * stream_array_linear = Hippo.allocate_aligned_region(1024 * 1024, PAGE_SIZE);
|
||||||
|
/* Stream Array (Pri/Sec): 4K */
|
||||||
|
void * stream_array_pri_sec = Hippo.allocate_page();
|
||||||
|
/* Transfer Ring Segments: 64K */
|
||||||
|
void * transfer_ring_segments = Hippo.allocate_aligned_region(64 * 1024, 64 * 1024);
|
||||||
|
/* Command Ring Segments: 64K */
|
||||||
|
void * command_ring_segments = Hippo.allocate_aligned_region(64 * 1024, 64 * 1024);
|
||||||
|
/* Event Ring Segments: 64K */
|
||||||
|
void * event_ring_segments = Hippo.allocate_aligned_region(64 * 1024, 64 * 1024);
|
||||||
|
/* Event Ring Segment Table: 512K */
|
||||||
|
void * event_ring_segment_table = Hippo.allocate_aligned_region(512 * 1024, PAGE_SIZE);
|
||||||
|
/* Scratchpad Buffer Array: 248 bytes */
|
||||||
|
void ** scratchpad_buffer_array = cast(void **)Hippo.allocate_aligned_region(2 * PAGE_SIZE, PAGE_SIZE);
|
||||||
|
|
||||||
|
size_t n_scratchpad_buffers = m_capability_registers.max_scratchpad_buffers;
|
||||||
|
for (size_t i = 0u; i < n_scratchpad_buffers; i++)
|
||||||
|
{
|
||||||
|
void * scratchpad_buffer = Hippo.allocate_page();
|
||||||
|
scratchpad_buffer_array[i] = scratchpad_buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
device_context_index[0] = scratchpad_buffer_array;
|
||||||
|
|
||||||
|
size_t max_slots = m_capability_registers.max_slots;
|
||||||
|
for (size_t i = 0u; i < max_slots; i++)
|
||||||
|
{
|
||||||
|
void * addr;
|
||||||
|
if ((i & 1) == 0)
|
||||||
|
{
|
||||||
|
page = cast(ubyte *)Hippo.allocate_page();
|
||||||
|
addr = page;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
addr = &page[2048];
|
||||||
|
}
|
||||||
|
device_context_index[1 + i] = addr;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_operational_registers.dcbaap = cast(ulong)device_context_index;
|
||||||
|
/* TODO: write cr_ctrl. */
|
||||||
|
/+ TODO:
|
||||||
|
m_operational_registers.config = m_capability_registers.hcs_params1 & 0xFu;
|
||||||
|
m_operational_registers.dn_ctrl = 1u << 1u;
|
||||||
|
+/
|
||||||
|
/* TODO: set up interrupt register set. */
|
||||||
|
Klog.writefln("XHCI controller initialization successful");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void dump_extended_capabilities()
|
||||||
|
{
|
||||||
|
size_t ext_cap_off = (m_capability_registers.hcc_params1 >> 16u);
|
||||||
|
if (ext_cap_off != 0u)
|
||||||
|
{
|
||||||
|
Klog.writefln("Extended capabilities:");
|
||||||
|
uint * extended_capabilities = cast(uint *)m_capability_registers + ext_cap_off;
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
uint ec0 = extended_capabilities[0];
|
||||||
|
size_t next_offset = (ec0 >> 8u) & 0xFFu;
|
||||||
|
size_t len = next_offset;
|
||||||
|
if (len < 1u)
|
||||||
|
{
|
||||||
|
len = 1u;
|
||||||
|
}
|
||||||
|
ubyte type = ec0 & 0xFFu;
|
||||||
|
if (type == 2u)
|
||||||
|
{
|
||||||
|
len = 4u;
|
||||||
|
}
|
||||||
|
for (size_t i = 0u; i < len; i++)
|
||||||
|
{
|
||||||
|
uint ecv = extended_capabilities[i];
|
||||||
|
Klog.writef(" %02X %02X %02X %02X",
|
||||||
|
ecv & 0xFFu,
|
||||||
|
(ecv >> 8u) & 0xFFu,
|
||||||
|
(ecv >> 16u) & 0xFFu,
|
||||||
|
(ecv >> 24u) & 0xFFu);
|
||||||
|
}
|
||||||
|
Klog.writef("\n");
|
||||||
|
if (next_offset == 0u)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
extended_capabilities += next_offset;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool reset()
|
||||||
|
{
|
||||||
|
/* Clear run/stop bit. */
|
||||||
|
m_operational_registers.usb_command &= ~0x1u;
|
||||||
|
|
||||||
|
/* Wait for HCHalted bit to be set. */
|
||||||
|
enum halt_timeout = 40u;
|
||||||
|
for (size_t halt_timeout_counter = 0u;;)
|
||||||
|
{
|
||||||
|
Time.msleep(1u);
|
||||||
|
|
||||||
|
if ((m_operational_registers.usb_status & 1u) != 0u)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
halt_timeout_counter++;
|
||||||
|
if (halt_timeout_counter >= halt_timeout)
|
||||||
|
{
|
||||||
|
Klog.writefln("XHCI controller failed to halt");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Set HC reset bit. */
|
||||||
|
m_operational_registers.usb_command |= (1u << 1u);
|
||||||
|
|
||||||
|
/* Wait for controller to be ready. */
|
||||||
|
enum reset_timeout = 100u;
|
||||||
|
for (size_t reset_timeout_counter = 0u;;)
|
||||||
|
{
|
||||||
|
Time.msleep(1u);
|
||||||
|
|
||||||
|
if (((m_operational_registers.usb_command & (1u << 1u)) == 0u) &&
|
||||||
|
((m_operational_registers.usb_status & (1u << 11u)) == 0u))
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
reset_timeout_counter++;
|
||||||
|
if (reset_timeout_counter >= reset_timeout)
|
||||||
|
{
|
||||||
|
Klog.writefln("XHCI controller failed to reset");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void build(Pci.Device * pci_device)
|
||||||
|
{
|
||||||
|
if (pci_device.memory_ranges[0].address != null)
|
||||||
|
{
|
||||||
|
XHCI * xhci = A1.allocate!XHCI();
|
||||||
|
xhci.initialize(pci_device);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
/**
|
||||||
|
* HULK utility functions.
|
||||||
|
*/
|
||||||
|
module hulk.util;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Round a value up to the next power of 2.
|
||||||
|
*
|
||||||
|
* @param v Value to round up.
|
||||||
|
* @param p Power of 2 to round up to.
|
||||||
|
*
|
||||||
|
* @return Rounded up value.
|
||||||
|
*/
|
||||||
|
ulong round_up_power_2(ulong v, ulong p)
|
||||||
|
{
|
||||||
|
return (v + p - 1u) & ~(p - 1u);
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
module hulk.ver;
|
||||||
|
|
||||||
|
enum VERSION = "0.18.0";
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
module hulk.volatile;
|
||||||
|
|
||||||
|
import core.volatile;
|
||||||
|
|
||||||
|
struct Volatile(T)
|
||||||
|
{
|
||||||
|
private T value;
|
||||||
|
|
||||||
|
public @property T read()
|
||||||
|
{
|
||||||
|
return volatileLoad(&value);
|
||||||
|
}
|
||||||
|
alias read this;
|
||||||
|
|
||||||
|
public void opAssign(T value)
|
||||||
|
{
|
||||||
|
volatileStore(&this.value, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public T opUnary(string s)()
|
||||||
|
{
|
||||||
|
T v = read();
|
||||||
|
mixin(s ~ "v;");
|
||||||
|
volatileStore(&this.value, v);
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T opOpAssign(string s)(T rhs)
|
||||||
|
{
|
||||||
|
T v = read();
|
||||||
|
mixin("v " ~ s ~ "= rhs;");
|
||||||
|
volatileStore(&this.value, v);
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,191 @@
|
|||||||
|
/**
|
||||||
|
* HULK Formatted stream writing support.
|
||||||
|
*/
|
||||||
|
module hulk.writef;
|
||||||
|
|
||||||
|
import core.stdc.stdarg;
|
||||||
|
|
||||||
|
alias ch_out_fn = void function(ubyte);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Format a string and write characters to the given output function.
|
||||||
|
*
|
||||||
|
* @param ch_out Character output function.
|
||||||
|
* @param s Format string.
|
||||||
|
* @param args Variable arguments structure.
|
||||||
|
*
|
||||||
|
* @return Number of characters written.
|
||||||
|
*/
|
||||||
|
size_t writefv(ch_out_fn ch_out, string s, va_list args)
|
||||||
|
{
|
||||||
|
size_t length_written;
|
||||||
|
bool escape = false;
|
||||||
|
char pad = ' ';
|
||||||
|
size_t width;
|
||||||
|
foreach (char c; s)
|
||||||
|
{
|
||||||
|
if (escape)
|
||||||
|
{
|
||||||
|
if (c == 'c')
|
||||||
|
{
|
||||||
|
ulong ch;
|
||||||
|
va_arg(args, ch);
|
||||||
|
ch_out(cast(ubyte)ch);
|
||||||
|
length_written++;
|
||||||
|
escape = false;
|
||||||
|
}
|
||||||
|
else if (c == 'p')
|
||||||
|
{
|
||||||
|
ulong v;
|
||||||
|
va_arg(args, v);
|
||||||
|
length_written += write_hex(v, true, true, pad, width, ch_out);
|
||||||
|
escape = false;
|
||||||
|
}
|
||||||
|
else if (c == 'x')
|
||||||
|
{
|
||||||
|
ulong v;
|
||||||
|
va_arg(args, v);
|
||||||
|
length_written += write_hex(v, false, false, pad, width, ch_out);
|
||||||
|
escape = false;
|
||||||
|
}
|
||||||
|
else if (c == 'X')
|
||||||
|
{
|
||||||
|
ulong v;
|
||||||
|
va_arg(args, v);
|
||||||
|
length_written += write_hex(v, true, false, pad, width, ch_out);
|
||||||
|
escape = false;
|
||||||
|
}
|
||||||
|
else if (c == 'u')
|
||||||
|
{
|
||||||
|
ulong v;
|
||||||
|
va_arg(args, v);
|
||||||
|
length_written += write_udec(v, pad, width, ch_out);
|
||||||
|
escape = false;
|
||||||
|
}
|
||||||
|
else if ((c == '0') && (width == 0u))
|
||||||
|
{
|
||||||
|
pad = '0';
|
||||||
|
}
|
||||||
|
else if (('0' <= c) && (c <= '9'))
|
||||||
|
{
|
||||||
|
width = (width * 10u) + (c - '0');
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ch_out(c);
|
||||||
|
escape = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (c == '%')
|
||||||
|
{
|
||||||
|
escape = true;
|
||||||
|
pad = ' ';
|
||||||
|
width = 0u;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ch_out(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return length_written;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Format a string and write characters to the given output function.
|
||||||
|
*
|
||||||
|
* @param ch_out Character output function.
|
||||||
|
* @param s Format string.
|
||||||
|
*
|
||||||
|
* @return Number of characters written.
|
||||||
|
*/
|
||||||
|
extern(C) size_t writef(ch_out_fn ch_out, string s, ...)
|
||||||
|
{
|
||||||
|
va_list args;
|
||||||
|
va_start(args, s);
|
||||||
|
size_t rv = writefv(ch_out, s, args);
|
||||||
|
va_end(args);
|
||||||
|
return rv;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Format a value in hexadecimal to the given output function.
|
||||||
|
*
|
||||||
|
* @param v Value to format.
|
||||||
|
* @param upper Whether to use uppercase letters.
|
||||||
|
* @param pointer Whether to format as a pointer.
|
||||||
|
* @param pad Pad character to use.
|
||||||
|
* @param width Field width.
|
||||||
|
* @param ch_out Character output function.
|
||||||
|
*
|
||||||
|
* @return Number of characters written.
|
||||||
|
*/
|
||||||
|
private size_t write_hex(ulong v, bool upper, bool pointer, char pad, size_t width, ch_out_fn ch_out)
|
||||||
|
{
|
||||||
|
static __gshared const(char)[16] hex_chars_lower = "0123456789abcdef";
|
||||||
|
static __gshared const(char)[16] hex_chars_upper = "0123456789ABCDEF";
|
||||||
|
char[19] buf;
|
||||||
|
size_t digit;
|
||||||
|
size_t buf_idx;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
ulong n = v & 0xFu;
|
||||||
|
buf[buf_idx] = upper ? hex_chars_upper[n] : hex_chars_lower[n];
|
||||||
|
v >>= 4u;
|
||||||
|
buf_idx++;
|
||||||
|
digit++;
|
||||||
|
if (pointer && ((digit % 4) == 0) && (v != 0u))
|
||||||
|
{
|
||||||
|
buf[buf_idx] = '_';
|
||||||
|
buf_idx++;
|
||||||
|
}
|
||||||
|
} while (v != 0u);
|
||||||
|
size_t length_written = buf_idx;
|
||||||
|
while (width > buf_idx)
|
||||||
|
{
|
||||||
|
ch_out(pad);
|
||||||
|
width--;
|
||||||
|
length_written++;
|
||||||
|
}
|
||||||
|
do
|
||||||
|
{
|
||||||
|
buf_idx--;
|
||||||
|
ch_out(buf[buf_idx]);
|
||||||
|
} while (buf_idx != 0u);
|
||||||
|
return length_written;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Format a value in decimal to the given output function.
|
||||||
|
*
|
||||||
|
* @param v Value to format.
|
||||||
|
* @param pad Pad character to use.
|
||||||
|
* @param width Field width.
|
||||||
|
* @param ch_out Character output function.
|
||||||
|
*
|
||||||
|
* @return Number of characters written.
|
||||||
|
*/
|
||||||
|
private size_t write_udec(ulong v, char pad, size_t width, ch_out_fn ch_out)
|
||||||
|
{
|
||||||
|
char[20] buf;
|
||||||
|
size_t i;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
ulong d = v % 10u;
|
||||||
|
buf[i] = cast(char)(d + '0');
|
||||||
|
v /= 10u;
|
||||||
|
i++;
|
||||||
|
} while (v != 0u);
|
||||||
|
size_t length_written = i;
|
||||||
|
while (width > i)
|
||||||
|
{
|
||||||
|
ch_out(pad);
|
||||||
|
width--;
|
||||||
|
length_written++;
|
||||||
|
}
|
||||||
|
do
|
||||||
|
{
|
||||||
|
i--;
|
||||||
|
ch_out(buf[i]);
|
||||||
|
} while (i != 0u);
|
||||||
|
return length_written;
|
||||||
|
}
|
||||||
-228
@@ -1,228 +0,0 @@
|
|||||||
|
|
||||||
%include "bootdef.inc"
|
|
||||||
|
|
||||||
[bits 16]
|
|
||||||
|
|
||||||
org 0x7c00
|
|
||||||
|
|
||||||
jmp short start
|
|
||||||
|
|
||||||
; --------------------------------------------------
|
|
||||||
; data portion of the "DOS BOOT RECORD"
|
|
||||||
; ----------------------------------------------------------------------
|
|
||||||
brINT13Flag DB 90H ; 0002h - 0EH for INT13 AH=42 READ
|
|
||||||
brOEM DB 'MSDOS5.0' ; 0003h - OEM ID - Windows 95B
|
|
||||||
brBPS DW 512 ; 000Bh - Bytes per sector
|
|
||||||
brSPC DB 1 ; 000Dh - Sector per cluster
|
|
||||||
brSc_b4_fat DW 1 ; 000Eh - Reserved sectors
|
|
||||||
brFATs DB 2 ; 0010h - FAT copies
|
|
||||||
brRootEntries DW 0E0H ; 0011h - Root directory entries
|
|
||||||
brSectorCount DW 2880 ; 0013h - Sectors in volume, < 32MB
|
|
||||||
brMedia DB 240 ; 0015h - Media descriptor
|
|
||||||
brSPF DW 9 ; 0016h - Sectors per FAT
|
|
||||||
brSc_p_trk DW 18 ; 0018h - Sectors per head/track
|
|
||||||
brHPC DW 2 ; 001Ah - Heads per cylinder
|
|
||||||
brSc_b4_prt DD 0 ; 001Ch - Hidden sectors
|
|
||||||
brSectors DD 0 ; 0020h - Total number of sectors
|
|
||||||
brDrive DB 0 ; 0024h - Physical drive no.
|
|
||||||
DB 0 ; 0025h - Reserved (FAT32)
|
|
||||||
DB 29H ; 0026h - Extended boot record sig (FAT32)
|
|
||||||
brSerialNum DD 404418EAH ; 0027h - Volume serial number
|
|
||||||
brLabel DB 'Holtrops OS' ; 002Bh - Volume label
|
|
||||||
brFSID DB 'FAT12 ' ; 0036h - File System ID
|
|
||||||
;------------------------------------------------------------------------
|
|
||||||
|
|
||||||
start:
|
|
||||||
jmp 0:jmphere ;ensure that cs=0 and ip=0x7c00...
|
|
||||||
jmphere:
|
|
||||||
;dl=drive number, save it!
|
|
||||||
xor ax, ax
|
|
||||||
mov ds, ax
|
|
||||||
mov [brDrive], dl
|
|
||||||
cli
|
|
||||||
mov ss, ax
|
|
||||||
mov sp, 0x7Bfe ;right under boot sector
|
|
||||||
sti
|
|
||||||
|
|
||||||
mov ax, 0xb800
|
|
||||||
mov ds, ax
|
|
||||||
mov es, ax
|
|
||||||
|
|
||||||
xor di, di
|
|
||||||
mov ax, 0x0700
|
|
||||||
mov cx, 2000
|
|
||||||
cls:
|
|
||||||
stosw
|
|
||||||
loop cls
|
|
||||||
|
|
||||||
enable_a20:
|
|
||||||
in al, 0x64
|
|
||||||
test al, 2
|
|
||||||
jnz enable_a20
|
|
||||||
mov al, 0xD1
|
|
||||||
out 0x64, al
|
|
||||||
ea20_2: in al, 0x64
|
|
||||||
and ax, byte 2
|
|
||||||
jnz ea20_2
|
|
||||||
mov al, 0xDF
|
|
||||||
out 0x60, al
|
|
||||||
|
|
||||||
unreal:
|
|
||||||
xor ax, ax
|
|
||||||
mov es, ax
|
|
||||||
mov ds, ax
|
|
||||||
|
|
||||||
lgdt [gdtr] ;load gdt
|
|
||||||
cli
|
|
||||||
push es
|
|
||||||
push ds ;save segment values
|
|
||||||
mov ebx, cr0
|
|
||||||
inc bl
|
|
||||||
mov cr0, ebx ;pmode!
|
|
||||||
mov ax, KERNEL_DATA
|
|
||||||
mov es, ax
|
|
||||||
mov ds, ax ;load segment limits
|
|
||||||
dec bl
|
|
||||||
mov cr0, ebx ;back to real mode!
|
|
||||||
pop ds
|
|
||||||
pop es ;segments back, with 4gb limits!
|
|
||||||
sti
|
|
||||||
|
|
||||||
;now lets read in the FAT and root directory so we can search for the stage2 file...
|
|
||||||
mov ax, 0x0209 ;FAT1
|
|
||||||
mov cx, 0x0002
|
|
||||||
xor dh, dh
|
|
||||||
mov dl, [brDrive]
|
|
||||||
mov bx, BOOT_FAT_SEG
|
|
||||||
mov es, bx
|
|
||||||
xor bx, bx
|
|
||||||
int 0x13
|
|
||||||
|
|
||||||
mov ax, 0x020E ;root directory
|
|
||||||
mov cx, 0x0002 ;cyl/sect
|
|
||||||
mov dh, 0x01 ;head
|
|
||||||
mov dl, [brDrive] ;drive
|
|
||||||
mov bx, BOOT_ROOT_SEG
|
|
||||||
mov es, bx
|
|
||||||
xor bx, bx
|
|
||||||
int 0x13
|
|
||||||
|
|
||||||
;k now read root directory
|
|
||||||
mov bx, BOOT_ROOT_SEG
|
|
||||||
mov ds, bx
|
|
||||||
xor si, si ;k now ds:si points to beginning of root directory
|
|
||||||
mov es, si
|
|
||||||
mov cx, 224 ;max root entries
|
|
||||||
loop_compare:
|
|
||||||
mov di, stage2
|
|
||||||
push cx
|
|
||||||
push si ;save pointer to root dir entry
|
|
||||||
mov cx, 11
|
|
||||||
loop_name:
|
|
||||||
cmpsb
|
|
||||||
loopz loop_name
|
|
||||||
jnz goon ;cx didn't get to zero, bad file
|
|
||||||
pop si
|
|
||||||
pop cx
|
|
||||||
jmp found_file ;good file, ds:si points to start of root directory entry
|
|
||||||
goon:
|
|
||||||
pop si
|
|
||||||
pop cx
|
|
||||||
add si, 32
|
|
||||||
loop loop_compare
|
|
||||||
|
|
||||||
error:
|
|
||||||
jmp $ ;halt! no kernel file found!
|
|
||||||
|
|
||||||
found_file: ;ds:si points to root dir entry
|
|
||||||
xor ax, ax
|
|
||||||
mov gs, ax
|
|
||||||
mov ax, BOOT_STAGE2_SEG
|
|
||||||
mov es, ax
|
|
||||||
|
|
||||||
mov ax, [ds:si+26]
|
|
||||||
mov bx, BOOT_FAT_SEG
|
|
||||||
mov ds, bx ;ds points to beginning of FAT
|
|
||||||
xor di, di
|
|
||||||
|
|
||||||
readstage2_loop:
|
|
||||||
cmp ax, 0xff7
|
|
||||||
jg readstage2_done
|
|
||||||
inc di
|
|
||||||
push ax
|
|
||||||
call getCHSfromCluster
|
|
||||||
mov ax, 0x0201
|
|
||||||
mov dl, [gs:BOOT_DRIVE]
|
|
||||||
xor bx, bx
|
|
||||||
int 0x13
|
|
||||||
mov bx, es
|
|
||||||
add bx, 0x0020
|
|
||||||
mov es, bx
|
|
||||||
pop ax ;current logical cluster #
|
|
||||||
|
|
||||||
mov cx, ax ;cx=logical cluster
|
|
||||||
mov dx, 3
|
|
||||||
mul dx
|
|
||||||
shr ax, 1 ;ax=logical cluster * 3 / 2
|
|
||||||
mov si, ax
|
|
||||||
test cl, 1 ;is bit0 set?
|
|
||||||
jnz odd_cluster
|
|
||||||
even_cluster:
|
|
||||||
lodsw
|
|
||||||
and ax, 0x0fff
|
|
||||||
jmp got_cluster
|
|
||||||
odd_cluster:
|
|
||||||
lodsw
|
|
||||||
shr ax, 4
|
|
||||||
got_cluster:
|
|
||||||
jmp readstage2_loop
|
|
||||||
|
|
||||||
readstage2_done:
|
|
||||||
|
|
||||||
jmp 0:BOOT_STAGE2_ADD
|
|
||||||
|
|
||||||
;------------------------------------------------------
|
|
||||||
getCHSfromCluster:
|
|
||||||
;input: ax=lba of sector on floppy (0-2879)
|
|
||||||
add ax, 31 ;convert logical cluster# to lba#
|
|
||||||
xor dx, dx ;lba->chs
|
|
||||||
mov bx, 18
|
|
||||||
div bx
|
|
||||||
inc dx
|
|
||||||
mov cl, dl ;sector# (1-18)
|
|
||||||
xor dx, dx
|
|
||||||
mov bx, 2
|
|
||||||
div bx
|
|
||||||
mov ch, al ;cylinder# (0-79)
|
|
||||||
mov dh, dl ;head# (0-1)
|
|
||||||
ret
|
|
||||||
|
|
||||||
|
|
||||||
;-------------------------------------------------------
|
|
||||||
gdtr:
|
|
||||||
dw gdt_end-gdt-1
|
|
||||||
dd gdt
|
|
||||||
gdt:
|
|
||||||
dd 0
|
|
||||||
dd 0
|
|
||||||
|
|
||||||
KERNEL_DATA equ $-gdt
|
|
||||||
db 0xff ;segment 16 = 4gb data
|
|
||||||
db 0xff
|
|
||||||
db 0x00
|
|
||||||
db 0x00
|
|
||||||
db 0x00
|
|
||||||
db 0x92
|
|
||||||
db 0xcf ;cf
|
|
||||||
db 0x00
|
|
||||||
|
|
||||||
gdt_end:
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
stage2: db "STAGE2 BIN"
|
|
||||||
|
|
||||||
times 510-($-$$) db 0
|
|
||||||
|
|
||||||
db 0x55, 0xaa
|
|
||||||
|
|
||||||
-921
@@ -1,921 +0,0 @@
|
|||||||
|
|
||||||
%include "bootdef.inc"
|
|
||||||
|
|
||||||
%define VESA_MODEINFO_SEG 0x0120
|
|
||||||
%define VESA_MODELIST_SEG 0x0140
|
|
||||||
%define GOOD_MODELIST_SEG 0x0160
|
|
||||||
|
|
||||||
[bits 16]
|
|
||||||
|
|
||||||
org BOOT_STAGE2_ADD
|
|
||||||
|
|
||||||
|
|
||||||
;k now read root directory
|
|
||||||
mov bx, BOOT_ROOT_SEG
|
|
||||||
mov ds, bx
|
|
||||||
xor si, si ;k now ds:si points to beginning of root directory
|
|
||||||
mov es, si
|
|
||||||
mov cx, 224 ;max root entries
|
|
||||||
loop_compare:
|
|
||||||
mov di, kernel
|
|
||||||
push cx
|
|
||||||
push si ;save pointer to root dir entry
|
|
||||||
mov cx, 11
|
|
||||||
loop_name:
|
|
||||||
cmpsb
|
|
||||||
loopz loop_name
|
|
||||||
jnz goon ;cx didn't get to zero, bad file
|
|
||||||
pop si
|
|
||||||
pop cx
|
|
||||||
jmp found_file ;good file, ds:si points to start of root directory entry
|
|
||||||
goon:
|
|
||||||
pop si
|
|
||||||
add si, 32
|
|
||||||
pop cx
|
|
||||||
loop loop_compare
|
|
||||||
|
|
||||||
error:
|
|
||||||
jmp $ ;halt! no kernel file found!
|
|
||||||
|
|
||||||
found_file: ;ds:si points to root dir entry of kernel file
|
|
||||||
xor ax, ax
|
|
||||||
mov gs, ax
|
|
||||||
mov ax, [ds:si+26]
|
|
||||||
mov bx, BOOT_FAT_SEG
|
|
||||||
mov ds, bx ;ds points to beginning of FAT
|
|
||||||
mov edi, BOOT_KERNEL_ADD
|
|
||||||
|
|
||||||
readkernel_loop:
|
|
||||||
cmp ax, 0xff7
|
|
||||||
jg readkernel_done
|
|
||||||
push ax
|
|
||||||
call getCHSfromCluster
|
|
||||||
mov ax, 0x0201
|
|
||||||
mov dl, [gs:BOOT_DRIVE]
|
|
||||||
mov bx, BOOT_KERNEL_SEG
|
|
||||||
mov es, bx
|
|
||||||
xor bx, bx
|
|
||||||
int 0x13
|
|
||||||
mov cx, 256
|
|
||||||
xor ax, ax
|
|
||||||
mov es, ax
|
|
||||||
mov esi, BOOT_KERNEL_SEG*16
|
|
||||||
copykernel_loop:
|
|
||||||
mov ax, [es:esi]
|
|
||||||
mov [es:edi], ax
|
|
||||||
inc esi
|
|
||||||
inc esi
|
|
||||||
inc edi
|
|
||||||
inc edi
|
|
||||||
loop copykernel_loop
|
|
||||||
|
|
||||||
pop ax ;current logical cluster #
|
|
||||||
|
|
||||||
mov cx, ax ;cx=logical cluster
|
|
||||||
mov dx, 3
|
|
||||||
mul dx
|
|
||||||
shr ax, 1 ;ax=logical cluster * 3 / 2
|
|
||||||
mov si, ax
|
|
||||||
test cl, 1 ;is bit0 set?
|
|
||||||
jnz odd_cluster
|
|
||||||
even_cluster:
|
|
||||||
lodsw
|
|
||||||
and ax, 0x0fff
|
|
||||||
jmp got_cluster
|
|
||||||
odd_cluster:
|
|
||||||
lodsw
|
|
||||||
shr ax, 4
|
|
||||||
got_cluster:
|
|
||||||
jmp readkernel_loop
|
|
||||||
|
|
||||||
;------------------------------------------------------
|
|
||||||
readkernel_done: ;-------------put more real mode init stuff here!
|
|
||||||
;----ask to load RD from floppy
|
|
||||||
mov ax, 0xb800
|
|
||||||
mov es, ax
|
|
||||||
xor ax, ax
|
|
||||||
mov ds, ax
|
|
||||||
xor di, di
|
|
||||||
mov cx, 2000
|
|
||||||
mov ax, 0x0700
|
|
||||||
cls_loop:
|
|
||||||
stosw
|
|
||||||
loop cls_loop
|
|
||||||
|
|
||||||
mov dx, 0x3d4 ;move cursor off screen...
|
|
||||||
mov al, 0x0e
|
|
||||||
out dx, al
|
|
||||||
inc dx
|
|
||||||
mov al, 0xff
|
|
||||||
out dx, al
|
|
||||||
dec dx
|
|
||||||
mov al, 0x0f
|
|
||||||
out dx, al
|
|
||||||
inc dx
|
|
||||||
out dx, al
|
|
||||||
|
|
||||||
xor di, di
|
|
||||||
mov si, txt_welcome
|
|
||||||
mov ah, 0x1f
|
|
||||||
call puts
|
|
||||||
|
|
||||||
mov di, 160
|
|
||||||
mov si, txt_rd1
|
|
||||||
mov ah, 7
|
|
||||||
call puts
|
|
||||||
|
|
||||||
mov si, txt_rd2
|
|
||||||
mov di, 160*2
|
|
||||||
call puts
|
|
||||||
|
|
||||||
mov di, 160*3
|
|
||||||
mov si, txt_input
|
|
||||||
call puts
|
|
||||||
|
|
||||||
get_rd:
|
|
||||||
xor ax, ax
|
|
||||||
int 0x16
|
|
||||||
cmp al, '1'
|
|
||||||
jz got_rd
|
|
||||||
cmp al, '2'
|
|
||||||
jnz get_rd
|
|
||||||
got_rd:
|
|
||||||
stosb
|
|
||||||
sub al, '1'
|
|
||||||
push ds
|
|
||||||
mov bx, BOOT_DATA_SEG ;segment for data to send kernel
|
|
||||||
mov ds, bx
|
|
||||||
mov [ds:BOOT_HASRD], al
|
|
||||||
pop ds ;ds=0
|
|
||||||
cmp al, 0 ;dont load rd
|
|
||||||
jz no_rd
|
|
||||||
|
|
||||||
mov cx, 80
|
|
||||||
mov edi, 0xb8000+160*4
|
|
||||||
filler_loop:
|
|
||||||
mov word [ds:edi], 0x0400+177
|
|
||||||
inc edi
|
|
||||||
inc edi
|
|
||||||
loop filler_loop
|
|
||||||
mov cx, 80 ;80 cylinders to read
|
|
||||||
xor si, si
|
|
||||||
mov edi, BOOT_RD_ADD ;ram disk address
|
|
||||||
read_cylinder:
|
|
||||||
push cx
|
|
||||||
mov bx, 0x0100
|
|
||||||
mov es, bx
|
|
||||||
xor bx, bx
|
|
||||||
mov ax, 0x0224
|
|
||||||
mov cx, si
|
|
||||||
mov ch, cl
|
|
||||||
mov cl, 1
|
|
||||||
xor dx, dx
|
|
||||||
mov dl, [gs:BOOT_DRIVE]
|
|
||||||
int 0x13
|
|
||||||
|
|
||||||
mov ebx, 0xb8000
|
|
||||||
add bx, si
|
|
||||||
shl bl, 1
|
|
||||||
mov word [ds:ebx+160*4], 0x0200+219
|
|
||||||
|
|
||||||
push si
|
|
||||||
mov esi, 0x1000
|
|
||||||
mov cx, 0x2400
|
|
||||||
copydisk_loop:
|
|
||||||
mov ax, [ds:esi]
|
|
||||||
inc esi
|
|
||||||
inc esi
|
|
||||||
mov [ds:edi], ax
|
|
||||||
inc edi
|
|
||||||
inc edi
|
|
||||||
loop copydisk_loop
|
|
||||||
|
|
||||||
pop si ;what cylinder# we are on...
|
|
||||||
inc si
|
|
||||||
pop cx
|
|
||||||
loop read_cylinder
|
|
||||||
|
|
||||||
;------------------------------------------------------
|
|
||||||
no_rd:
|
|
||||||
mov ax, BOOT_DATA_SEG
|
|
||||||
mov es, ax
|
|
||||||
mov di, BOOT_MEMMAP ;store memmap info in es:di for kernel
|
|
||||||
xor edx, edx
|
|
||||||
mov ax, 0x0100
|
|
||||||
mov ds, ax
|
|
||||||
xor ebx, ebx
|
|
||||||
getmemmap_loop:
|
|
||||||
push es
|
|
||||||
push di
|
|
||||||
push edx
|
|
||||||
mov ax, 0x0100 ;get memory map
|
|
||||||
mov es, ax
|
|
||||||
xor di, di
|
|
||||||
mov eax, 0x0000E820
|
|
||||||
mov ecx, 0x00000014
|
|
||||||
;mov ebx, 0x00000000
|
|
||||||
mov edx, 0x534D4150 ;'SMAP'
|
|
||||||
int 0x15
|
|
||||||
jc getmemmap_carry
|
|
||||||
cmp eax, 0x534D4150 ;eax should be 'SMAP' on return...
|
|
||||||
jnz getmemmap_error
|
|
||||||
cmp ebx, 0
|
|
||||||
jz getmemmap_done
|
|
||||||
|
|
||||||
pop edx ;now, copy memmap entry to es:di on the stack
|
|
||||||
pop di
|
|
||||||
pop es
|
|
||||||
xor si, si
|
|
||||||
mov cx, 20
|
|
||||||
getmemmap_copymem_loop:
|
|
||||||
lodsb
|
|
||||||
stosb
|
|
||||||
loop getmemmap_copymem_loop
|
|
||||||
inc edx
|
|
||||||
jmp getmemmap_loop
|
|
||||||
|
|
||||||
getmemmap_error:
|
|
||||||
mov ax, 0xb800
|
|
||||||
mov es, ax
|
|
||||||
mov di, 160*24
|
|
||||||
xor ax, ax
|
|
||||||
mov ds, ax
|
|
||||||
mov si, txt_memerror
|
|
||||||
mov ah, 0x04
|
|
||||||
call puts
|
|
||||||
hlt
|
|
||||||
jmp $
|
|
||||||
|
|
||||||
getmemmap_carry:
|
|
||||||
dec edx
|
|
||||||
getmemmap_done:
|
|
||||||
pop edx
|
|
||||||
pop di
|
|
||||||
pop es
|
|
||||||
|
|
||||||
xor si, si
|
|
||||||
mov cx, 20
|
|
||||||
getmemmap_done_copymem_loop:
|
|
||||||
lodsb
|
|
||||||
stosb
|
|
||||||
loop getmemmap_done_copymem_loop
|
|
||||||
inc edx
|
|
||||||
|
|
||||||
mov di, BOOT_MEMENTRIES
|
|
||||||
mov [es:di], edx ;save # of memmap entries for kernel
|
|
||||||
|
|
||||||
;on to vesa info...
|
|
||||||
xor ax, ax
|
|
||||||
mov gs, ax
|
|
||||||
mov ds, ax
|
|
||||||
mov ax, 0xb800
|
|
||||||
mov es, ax
|
|
||||||
mov di, 160
|
|
||||||
mov cx, 2000-80
|
|
||||||
mov ax, 0x0700
|
|
||||||
cls_vesa_loop:
|
|
||||||
stosw
|
|
||||||
loop cls_vesa_loop
|
|
||||||
mov si, txt_vesa
|
|
||||||
mov di, 160*1
|
|
||||||
mov ah, 7
|
|
||||||
call puts
|
|
||||||
|
|
||||||
push di
|
|
||||||
mov ax, 0x0100
|
|
||||||
mov es, ax
|
|
||||||
xor di, di
|
|
||||||
mov dword [es:di], "2EBV"
|
|
||||||
mov ax, 0x4F00
|
|
||||||
|
|
||||||
int 0x10
|
|
||||||
pop di
|
|
||||||
cmp ax, 0x004F
|
|
||||||
jz vesa_good
|
|
||||||
mov si, txt_novesa
|
|
||||||
mov ax, 0xb800
|
|
||||||
mov es, ax
|
|
||||||
mov ah, 7
|
|
||||||
call puts
|
|
||||||
mov ax, BOOT_DATA_SEG
|
|
||||||
mov ds, ax
|
|
||||||
mov word [ds:BOOT_VESA], 0
|
|
||||||
jmp vesa_done
|
|
||||||
vesa_good:
|
|
||||||
mov ax, 0xb800
|
|
||||||
mov es, ax
|
|
||||||
mov ax, 0x0100
|
|
||||||
mov ds, ax
|
|
||||||
xor si, si
|
|
||||||
mov bx, [4]
|
|
||||||
mov al, bh
|
|
||||||
call puthex
|
|
||||||
mov al, '.'
|
|
||||||
stosb
|
|
||||||
mov al, 7
|
|
||||||
stosb
|
|
||||||
mov al, bl
|
|
||||||
call puthex
|
|
||||||
add di, 4
|
|
||||||
cmp bh, 2
|
|
||||||
jge vesa_good2
|
|
||||||
xor ax, ax
|
|
||||||
mov ds, ax
|
|
||||||
mov si, txt_vesaold
|
|
||||||
mov ah, 7
|
|
||||||
call puts
|
|
||||||
mov ax, BOOT_DATA_SEG
|
|
||||||
mov ds, ax
|
|
||||||
mov word [ds:BOOT_VESA], 0
|
|
||||||
jmp vesa_done
|
|
||||||
vesa_good2:
|
|
||||||
mov ebx, [6] ;something like 0x00000E60
|
|
||||||
mov edx, [14]
|
|
||||||
mov si, bx
|
|
||||||
shr ebx, 16
|
|
||||||
mov ds, bx ;ds:si points to null-terminated OEM identification string
|
|
||||||
mov ah, 2
|
|
||||||
push si
|
|
||||||
call puts
|
|
||||||
pop si
|
|
||||||
mov ax, BOOT_DATA_SEG
|
|
||||||
mov es, ax
|
|
||||||
mov di, BOOT_VESA_OEM
|
|
||||||
vesa_copyoem:
|
|
||||||
lodsb
|
|
||||||
stosb
|
|
||||||
or al, al
|
|
||||||
jnz vesa_copyoem
|
|
||||||
mov ax, 0x0100
|
|
||||||
mov ds, ax
|
|
||||||
xor si, si
|
|
||||||
mov di, BOOT_VESA_VBE
|
|
||||||
mov cx, 512
|
|
||||||
vesa_copyvbe:
|
|
||||||
lodsb
|
|
||||||
stosb
|
|
||||||
loop vesa_copyvbe
|
|
||||||
|
|
||||||
mov si, dx
|
|
||||||
shr edx, 16
|
|
||||||
mov ds, dx ;ds:si points to video mode list
|
|
||||||
mov ax, VESA_MODELIST_SEG
|
|
||||||
mov es, ax
|
|
||||||
xor di, di
|
|
||||||
vesa_copymodes:
|
|
||||||
lodsw
|
|
||||||
stosw
|
|
||||||
cmp ax, 0xffff
|
|
||||||
jnz vesa_copymodes
|
|
||||||
|
|
||||||
mov ax, GOOD_MODELIST_SEG
|
|
||||||
mov es, ax
|
|
||||||
xor di, di
|
|
||||||
mov cx, 256
|
|
||||||
mov ax, 0xffff
|
|
||||||
clear_good_mode_list_loop:
|
|
||||||
stosw
|
|
||||||
loop clear_good_mode_list_loop
|
|
||||||
|
|
||||||
mov ax, VESA_MODELIST_SEG
|
|
||||||
mov ds, ax
|
|
||||||
xor si, si ;ds:si points to video mode list where we can edit it :)
|
|
||||||
mov ax, GOOD_MODELIST_SEG
|
|
||||||
mov es, ax
|
|
||||||
xor di, di
|
|
||||||
xor dx, dx ;dx=what good mode # we are on
|
|
||||||
vesa_readmodeinfo_loop:
|
|
||||||
lodsw
|
|
||||||
cmp ax, 0xffff
|
|
||||||
jz vesa_endofmodes
|
|
||||||
push ax ;save mode#
|
|
||||||
call checkmode
|
|
||||||
cmp ax, 0
|
|
||||||
jz vesa_readmodeinfo_good
|
|
||||||
pop ax
|
|
||||||
jmp vesa_readmodeinfo_loop
|
|
||||||
vesa_readmodeinfo_good:
|
|
||||||
pop ax ;restore mode#
|
|
||||||
stosw
|
|
||||||
call vesa_showmodeinfo
|
|
||||||
inc dx
|
|
||||||
jmp vesa_readmodeinfo_loop
|
|
||||||
|
|
||||||
vesa_endofmodes: ;here we have a list of good modes at GOOD_MODELIST_SEG:0
|
|
||||||
xor ax, ax
|
|
||||||
mov ds, ax
|
|
||||||
mov si, txt_consolemode
|
|
||||||
mov ax, 0xb800
|
|
||||||
mov es, ax
|
|
||||||
mov di, 160*2
|
|
||||||
mov ah, 7
|
|
||||||
call puts
|
|
||||||
mov di, 160*3
|
|
||||||
mov cx, dx
|
|
||||||
mov al, 'b'
|
|
||||||
vesa_displaylabels:
|
|
||||||
stosb
|
|
||||||
push ax
|
|
||||||
mov al, 7
|
|
||||||
stosb
|
|
||||||
mov al, '.'
|
|
||||||
stosb
|
|
||||||
mov al, 7
|
|
||||||
stosb
|
|
||||||
pop ax
|
|
||||||
inc al
|
|
||||||
add di, 160-4
|
|
||||||
loop vesa_displaylabels ;done drawing screen of VESA choices, now ask for one
|
|
||||||
;valid options are 'a' through (al-1)
|
|
||||||
mov bl, al
|
|
||||||
xor ax, ax
|
|
||||||
mov ds, ax
|
|
||||||
mov di, 160*24
|
|
||||||
mov si, txt_input
|
|
||||||
mov ah, 14
|
|
||||||
call puts
|
|
||||||
vesa_getchoice:
|
|
||||||
xor ax, ax
|
|
||||||
int 0x16
|
|
||||||
cmp al, 'a'
|
|
||||||
jl vesa_getchoice
|
|
||||||
cmp al, bl
|
|
||||||
jge vesa_getchoice
|
|
||||||
stosb
|
|
||||||
push ax
|
|
||||||
mov al, 14
|
|
||||||
stosb
|
|
||||||
pop ax
|
|
||||||
xor ah, ah
|
|
||||||
sub ax, 'a'
|
|
||||||
cmp ax, 0
|
|
||||||
jz vesa_consolemode_only
|
|
||||||
mov cx, ax ;cx holds good mode# (1=first good vesa mode)
|
|
||||||
dec cx
|
|
||||||
mov ax, GOOD_MODELIST_SEG
|
|
||||||
mov ds, ax
|
|
||||||
shl cx, 1
|
|
||||||
mov si, cx ;ds:si points to word containing selected mode#
|
|
||||||
lodsw
|
|
||||||
mov cx, ax
|
|
||||||
mov dx, ax ;cx and dx hold mode#
|
|
||||||
|
|
||||||
mov ax, VESA_MODEINFO_SEG
|
|
||||||
mov es, ax
|
|
||||||
xor di, di
|
|
||||||
mov ax, 0x4F01
|
|
||||||
int 0x10
|
|
||||||
call checkvesa
|
|
||||||
|
|
||||||
mov ax, VESA_MODEINFO_SEG
|
|
||||||
mov ds, ax
|
|
||||||
xor si, si
|
|
||||||
mov ax, BOOT_DATA_SEG
|
|
||||||
mov es, ax
|
|
||||||
mov di, BOOT_VESA_INFO
|
|
||||||
mov cx, 256
|
|
||||||
vesa_copymodeinfo_loop:
|
|
||||||
lodsb
|
|
||||||
stosb
|
|
||||||
loop vesa_copymodeinfo_loop ;store ModeInfoBlock for current
|
|
||||||
|
|
||||||
mov [es:BOOT_VESA], dx ;store mode# for kernel
|
|
||||||
|
|
||||||
; mov ax, 0xb800
|
|
||||||
; mov es, ax
|
|
||||||
; xor di, di
|
|
||||||
; mov al, dh
|
|
||||||
; call puthex2
|
|
||||||
; mov al, dl
|
|
||||||
; call puthex2
|
|
||||||
|
|
||||||
mov bx, dx
|
|
||||||
or bx, 0x4000 ;set "use LFB" bit of mode#
|
|
||||||
mov ax, 0x4F02
|
|
||||||
int 0x10 ;switch to graphics mode!!!
|
|
||||||
call checkvesa
|
|
||||||
|
|
||||||
jmp vesa_done
|
|
||||||
|
|
||||||
vesa_consolemode_only:
|
|
||||||
mov ax, BOOT_DATA_SEG
|
|
||||||
mov es, ax
|
|
||||||
mov word [es:BOOT_VESA], 0
|
|
||||||
|
|
||||||
jmp vesa_done
|
|
||||||
|
|
||||||
;------------------------------------------------------
|
|
||||||
vesa_showmodeinfo:
|
|
||||||
pusha
|
|
||||||
push es
|
|
||||||
push ds
|
|
||||||
mov ax, VESA_MODEINFO_SEG
|
|
||||||
mov ds, ax
|
|
||||||
xor si, si
|
|
||||||
mov ax, 0xb800
|
|
||||||
mov es, ax
|
|
||||||
|
|
||||||
mov cx, dx
|
|
||||||
|
|
||||||
mov ax, 160
|
|
||||||
mul dx ;ax=160*mod#
|
|
||||||
add ax, 160*3+6 ;offset first line of modes and room on left for label
|
|
||||||
mov di, ax
|
|
||||||
|
|
||||||
mov ax, [ds:18]
|
|
||||||
call console_putDec
|
|
||||||
add di, 10
|
|
||||||
mov al, 'x'
|
|
||||||
call console_putChar
|
|
||||||
inc di
|
|
||||||
inc di
|
|
||||||
mov ax, [ds:20]
|
|
||||||
call console_putDec
|
|
||||||
add di, 10
|
|
||||||
mov al, 'x'
|
|
||||||
call console_putChar
|
|
||||||
inc di
|
|
||||||
inc di
|
|
||||||
xor ah, ah
|
|
||||||
mov al, [ds:25]
|
|
||||||
call console_putDec
|
|
||||||
add di, 8
|
|
||||||
mov al, [ds:0]
|
|
||||||
test al, 0x80
|
|
||||||
jz vesa_showmodeinfo_done
|
|
||||||
mov al, 'L'
|
|
||||||
call console_putChar
|
|
||||||
mov al, 'F'
|
|
||||||
call console_putChar
|
|
||||||
mov al, 'B'
|
|
||||||
call console_putChar
|
|
||||||
inc di
|
|
||||||
inc di
|
|
||||||
mov ebx, [ds:40]
|
|
||||||
mov eax, ebx
|
|
||||||
shr eax, 24
|
|
||||||
call puthex2
|
|
||||||
mov eax, ebx
|
|
||||||
shr eax, 16
|
|
||||||
call puthex2
|
|
||||||
mov al, bh
|
|
||||||
call puthex2
|
|
||||||
mov al, bl
|
|
||||||
call puthex2
|
|
||||||
vesa_showmodeinfo_done:
|
|
||||||
pop ds
|
|
||||||
pop es
|
|
||||||
popa
|
|
||||||
ret
|
|
||||||
|
|
||||||
;------------------------------------------------------
|
|
||||||
checkmode:
|
|
||||||
push bx
|
|
||||||
push cx
|
|
||||||
push dx
|
|
||||||
push es
|
|
||||||
push ds
|
|
||||||
push di
|
|
||||||
push si
|
|
||||||
mov cx, ax ;cx=modenumber
|
|
||||||
mov ax, VESA_MODEINFO_SEG
|
|
||||||
mov es, ax
|
|
||||||
xor di, di
|
|
||||||
mov ax, 0x4F01
|
|
||||||
int 0x10
|
|
||||||
call checkvesa
|
|
||||||
xor di, di ;es:di -> ModeInfoBlock struc
|
|
||||||
mov ax, [es:di] ;ModeAttributes
|
|
||||||
test al, 1 ;mode supported
|
|
||||||
jz vesa_modenogood
|
|
||||||
test al, 8 ;color mode
|
|
||||||
jz vesa_modenogood
|
|
||||||
test al, 0x10 ;graphics mode
|
|
||||||
jz vesa_modenogood
|
|
||||||
test al, 0x80 ;Linear Frame Buffer supported
|
|
||||||
jz vesa_modenogood
|
|
||||||
mov al, [es:di+25] ;BitsPerPixel
|
|
||||||
cmp al, 16
|
|
||||||
jz vesa_bppok
|
|
||||||
cmp al, 24
|
|
||||||
jz vesa_bppok
|
|
||||||
cmp al, 32
|
|
||||||
jnz vesa_modenogood
|
|
||||||
vesa_bppok:
|
|
||||||
mov ax, [es:di+18] ;XResolution
|
|
||||||
mov bx, [es:di+20] ;YResolution
|
|
||||||
cmp ax, 640 ;640x480
|
|
||||||
jnz res_goon1
|
|
||||||
cmp bx, 480
|
|
||||||
jnz vesa_modenogood
|
|
||||||
jmp vesa_modegood
|
|
||||||
res_goon1:
|
|
||||||
cmp ax, 800
|
|
||||||
jnz res_goon2
|
|
||||||
cmp bx, 600
|
|
||||||
jnz vesa_modenogood
|
|
||||||
jmp vesa_modegood
|
|
||||||
res_goon2:
|
|
||||||
cmp ax, 1024
|
|
||||||
jnz res_goon3
|
|
||||||
cmp bx, 768
|
|
||||||
jnz vesa_modenogood
|
|
||||||
jmp vesa_modegood
|
|
||||||
res_goon3:
|
|
||||||
cmp ax, 1280
|
|
||||||
jnz res_goon4
|
|
||||||
cmp bx, 1024
|
|
||||||
jz vesa_modegood
|
|
||||||
cmp bx, 960
|
|
||||||
jz vesa_modegood
|
|
||||||
jmp vesa_modenogood
|
|
||||||
res_goon4:
|
|
||||||
cmp ax, 1600
|
|
||||||
jnz vesa_modenogood
|
|
||||||
cmp bx, 1200
|
|
||||||
jnz vesa_modenogood
|
|
||||||
vesa_modegood:
|
|
||||||
pop si
|
|
||||||
pop di
|
|
||||||
pop ds
|
|
||||||
pop es
|
|
||||||
pop dx
|
|
||||||
pop cx
|
|
||||||
pop bx
|
|
||||||
xor ax, ax
|
|
||||||
ret
|
|
||||||
vesa_modenogood:
|
|
||||||
pop si
|
|
||||||
pop di
|
|
||||||
pop ds
|
|
||||||
pop es
|
|
||||||
pop dx
|
|
||||||
pop cx
|
|
||||||
pop bx
|
|
||||||
mov ax, 0xffff
|
|
||||||
ret
|
|
||||||
|
|
||||||
;------------------------------------------------------
|
|
||||||
vesa_done:
|
|
||||||
|
|
||||||
; xor ax, ax ;wait for keypress...
|
|
||||||
; int 0x16
|
|
||||||
|
|
||||||
jmp go_pm
|
|
||||||
|
|
||||||
;------------------------------------------------------
|
|
||||||
puts:
|
|
||||||
lodsb
|
|
||||||
or al, al
|
|
||||||
jz puts_done
|
|
||||||
stosb
|
|
||||||
mov al, ah
|
|
||||||
stosb
|
|
||||||
jmp puts
|
|
||||||
puts_done:
|
|
||||||
ret
|
|
||||||
|
|
||||||
;------------------------------------------------------
|
|
||||||
checkvesa:
|
|
||||||
cmp ax, 0x004F
|
|
||||||
jnz vesaerror
|
|
||||||
ret
|
|
||||||
vesaerror:
|
|
||||||
mov ax, 0xb800
|
|
||||||
mov es, ax
|
|
||||||
xor ax, ax
|
|
||||||
mov ds, ax
|
|
||||||
mov si, txt_vesaerror
|
|
||||||
mov di, 160*24
|
|
||||||
mov ah, 4
|
|
||||||
call puts
|
|
||||||
cli
|
|
||||||
hlt
|
|
||||||
|
|
||||||
;-------Function console_putDec
|
|
||||||
;input:
|
|
||||||
; AX = number to display
|
|
||||||
;output:
|
|
||||||
; number written in decimal to es:di
|
|
||||||
console_putDec:
|
|
||||||
pusha
|
|
||||||
xor dx, dx
|
|
||||||
xor bh, bh ;no characters written yet
|
|
||||||
mov cx, 10000
|
|
||||||
div cx ;ax=quotiont, dx=remainder
|
|
||||||
add ax, '0'
|
|
||||||
cmp ax, '0'
|
|
||||||
je .goon1
|
|
||||||
call console_putChar
|
|
||||||
mov bh, 1
|
|
||||||
|
|
||||||
.goon1:
|
|
||||||
mov ax, dx ;load remainder to ax
|
|
||||||
xor dx, dx
|
|
||||||
mov cx, 1000
|
|
||||||
div cx ;ax=quotiont, dx=remainder
|
|
||||||
add ax, '0'
|
|
||||||
cmp ax, '0'
|
|
||||||
je .goon11
|
|
||||||
call console_putChar
|
|
||||||
mov bh, 1
|
|
||||||
jmp .goon2
|
|
||||||
.goon11:
|
|
||||||
cmp bh, 0
|
|
||||||
je .goon2
|
|
||||||
call console_putChar
|
|
||||||
|
|
||||||
.goon2:
|
|
||||||
mov ax, dx ;load remainder to ax
|
|
||||||
xor dx, dx
|
|
||||||
mov cx, 100
|
|
||||||
div cx ;ax=quotiont, dx=remainder
|
|
||||||
add ax, '0'
|
|
||||||
cmp ax, '0'
|
|
||||||
je .goon21
|
|
||||||
call console_putChar
|
|
||||||
mov bh, 1
|
|
||||||
jmp .goon3
|
|
||||||
.goon21:
|
|
||||||
cmp bh, 0
|
|
||||||
je .goon3
|
|
||||||
call console_putChar
|
|
||||||
|
|
||||||
.goon3:
|
|
||||||
mov ax, dx ;load remainder to ax
|
|
||||||
xor dx, dx
|
|
||||||
mov cx, 10
|
|
||||||
div cx ;ax=quotiont, dx=remainder
|
|
||||||
add ax, '0'
|
|
||||||
cmp ax, '0'
|
|
||||||
je .goon31
|
|
||||||
call console_putChar
|
|
||||||
mov bh, 1
|
|
||||||
jmp .goon4
|
|
||||||
.goon31:
|
|
||||||
cmp bh, 0
|
|
||||||
je .goon4
|
|
||||||
call console_putChar
|
|
||||||
.goon4: ;here dx contains last remainder
|
|
||||||
mov ax, dx
|
|
||||||
add ax, '0'
|
|
||||||
call console_putChar
|
|
||||||
|
|
||||||
popa
|
|
||||||
ret
|
|
||||||
|
|
||||||
;------------------------------------------------------
|
|
||||||
console_putChar:
|
|
||||||
stosb
|
|
||||||
mov al, 7
|
|
||||||
stosb
|
|
||||||
ret
|
|
||||||
|
|
||||||
;------------------------------------------------------
|
|
||||||
puthex:
|
|
||||||
;es:di points to video memory
|
|
||||||
;al holds hex value
|
|
||||||
|
|
||||||
push ax
|
|
||||||
mov ah, al
|
|
||||||
shr ax, 4
|
|
||||||
and al, 0x0F
|
|
||||||
add al, '0'
|
|
||||||
cmp al, '9'
|
|
||||||
jle puthex_goon1
|
|
||||||
add al, 'A'-'9'-1
|
|
||||||
puthex_goon1:
|
|
||||||
cmp al, '0'
|
|
||||||
jz puthex_skipzero
|
|
||||||
stosb
|
|
||||||
mov al, 7
|
|
||||||
stosb
|
|
||||||
puthex_skipzero:
|
|
||||||
pop ax
|
|
||||||
push ax
|
|
||||||
and al, 0x0F
|
|
||||||
add al, '0'
|
|
||||||
cmp al, '9'
|
|
||||||
jle puthex_goon2
|
|
||||||
add al, 'A'-'9'-1
|
|
||||||
puthex_goon2:
|
|
||||||
stosb
|
|
||||||
mov al, 7
|
|
||||||
stosb
|
|
||||||
pop ax
|
|
||||||
ret
|
|
||||||
|
|
||||||
;------------------------------------------------------
|
|
||||||
puthex2:
|
|
||||||
;es:di points to video memory, always displays 2 characters!
|
|
||||||
;al holds hex value
|
|
||||||
|
|
||||||
push ax
|
|
||||||
mov ah, al
|
|
||||||
shr ax, 4
|
|
||||||
and al, 0x0F
|
|
||||||
add al, '0'
|
|
||||||
cmp al, '9'
|
|
||||||
jle puthex2_goon1
|
|
||||||
add al, 'A'-'9'-1
|
|
||||||
puthex2_goon1:
|
|
||||||
stosb
|
|
||||||
mov al, 7
|
|
||||||
stosb
|
|
||||||
pop ax
|
|
||||||
push ax
|
|
||||||
and al, 0x0F
|
|
||||||
add al, '0'
|
|
||||||
cmp al, '9'
|
|
||||||
jle puthex2_goon2
|
|
||||||
add al, 'A'-'9'-1
|
|
||||||
puthex2_goon2:
|
|
||||||
stosb
|
|
||||||
mov al, 7
|
|
||||||
stosb
|
|
||||||
pop ax
|
|
||||||
ret
|
|
||||||
|
|
||||||
;------------------------------------------------------
|
|
||||||
txt_welcome: db " Welcome to HOS v", VERSION, "! ", 0
|
|
||||||
txt_rd1: db "1. Do not load an initial ram disk", 0
|
|
||||||
txt_rd2: db "2. Load initial ram disk from floppy", 0
|
|
||||||
txt_input: db "Enter your selection: ", 0
|
|
||||||
txt_vesa: db "VESA version: ", 0
|
|
||||||
txt_vesaerror: db "VESA function call error! Halting system!", 0
|
|
||||||
txt_novesa: db "VESA not found. Starting in console mode...", 0
|
|
||||||
txt_vesaold: db "VESA version 2.0 required. Starting in console mode...", 0
|
|
||||||
txt_consolemode: db "a. Console mode only.", 0
|
|
||||||
txt_memerror: db "Extended Memory Map information unavailable! Halting system...", 0
|
|
||||||
|
|
||||||
;------------------------------------------------------
|
|
||||||
getCHSfromCluster:
|
|
||||||
;input: ax=lba of sector on floppy (0-2879)
|
|
||||||
add ax, 31 ;convert logical cluster# to lba#
|
|
||||||
xor dx, dx ;lba->chs
|
|
||||||
mov bx, 18
|
|
||||||
div bx
|
|
||||||
inc dx
|
|
||||||
mov cl, dl ;sector# (1-18)
|
|
||||||
xor dx, dx
|
|
||||||
mov bx, 2
|
|
||||||
div bx
|
|
||||||
mov ch, al ;cylinder# (0-79)
|
|
||||||
mov dh, dl ;head# (0-1)
|
|
||||||
ret
|
|
||||||
|
|
||||||
|
|
||||||
;-------------------------------------------------------
|
|
||||||
gdtr:
|
|
||||||
dw gdt_end-gdt-1
|
|
||||||
dd gdt
|
|
||||||
gdt:
|
|
||||||
dd 0
|
|
||||||
dd 0
|
|
||||||
|
|
||||||
KERNEL_CODE equ $-gdt
|
|
||||||
db 0xff ;limit 7:0
|
|
||||||
db 0xff ;limit 15:8
|
|
||||||
db 0x00 ;base 7:0
|
|
||||||
db 0x00 ;base 15:8
|
|
||||||
db 0x00 ;base 23:16
|
|
||||||
db 0x9a ;access
|
|
||||||
db 0xcf ;flags / limit 19:16
|
|
||||||
db 0x00 ;base 31:24
|
|
||||||
|
|
||||||
KERNEL_DATA equ $-gdt
|
|
||||||
db 0xff ;segment 16 = 4gb data
|
|
||||||
db 0xff
|
|
||||||
db 0x00
|
|
||||||
db 0x00
|
|
||||||
db 0x00
|
|
||||||
db 0x92
|
|
||||||
db 0xcf ;cf
|
|
||||||
db 0x00
|
|
||||||
|
|
||||||
gdt_end:
|
|
||||||
|
|
||||||
;------------------------------------------------------
|
|
||||||
go_pm:
|
|
||||||
xor ax, ax
|
|
||||||
mov ds, ax
|
|
||||||
lgdt [gdtr]
|
|
||||||
cli
|
|
||||||
mov eax, cr0
|
|
||||||
inc eax
|
|
||||||
mov cr0, eax
|
|
||||||
|
|
||||||
jmp KERNEL_CODE:pmode
|
|
||||||
|
|
||||||
bits 32
|
|
||||||
pmode:
|
|
||||||
mov ax, KERNEL_DATA
|
|
||||||
mov es, ax
|
|
||||||
mov ds, ax
|
|
||||||
mov fs, ax
|
|
||||||
mov gs, ax
|
|
||||||
jmp KERNEL_CODE:BOOT_KERNEL_ADD
|
|
||||||
|
|
||||||
kernel: db "KERNEL BIN", 0
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,258 +0,0 @@
|
|||||||
const byte stdfont[] = {
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xE0, 0xC0, 0x80,
|
|
||||||
0x00, 0x08, 0x18, 0x38, 0x78, 0x38, 0x18, 0x08,
|
|
||||||
0x00, 0x20, 0x70, 0xA8, 0x20, 0xA8, 0x70, 0x20,
|
|
||||||
0x00, 0xA0, 0xA0, 0xA0, 0xA0, 0x00, 0xA0, 0xA0,
|
|
||||||
0x00, 0x78, 0xA8, 0xA8, 0x78, 0x28, 0x28, 0x68,
|
|
||||||
0x00, 0x78, 0x80, 0x70, 0x88, 0x70, 0x08, 0xF0,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, 0xF8,
|
|
||||||
0x00, 0x20, 0x70, 0xA8, 0x20, 0xA8, 0x70, 0xF8,
|
|
||||||
0x00, 0x20, 0x70, 0xA8, 0x20, 0x20, 0x20, 0x20,
|
|
||||||
0x00, 0x20, 0x20, 0x20, 0x20, 0xA8, 0x70, 0x20,
|
|
||||||
0x00, 0x00, 0x20, 0x10, 0xF8, 0x10, 0x20, 0x00,
|
|
||||||
0x00, 0x00, 0x20, 0x40, 0xF8, 0x40, 0x20, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x60, 0x90, 0x60, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x20, 0x70, 0xF8, 0xF8, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0xF8, 0xF8, 0x70, 0x20, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x20, 0x20, 0x20, 0x20, 0x00, 0x20, 0x20,
|
|
||||||
0x00, 0x50, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x50, 0xF8, 0x50, 0x50, 0x50, 0xF8, 0x50,
|
|
||||||
0x00, 0x20, 0x78, 0xA0, 0x70, 0x28, 0xF0, 0x20,
|
|
||||||
0x00, 0x00, 0x08, 0x90, 0x20, 0x48, 0x80, 0x00,
|
|
||||||
0x00, 0x20, 0x50, 0x50, 0x20, 0x58, 0x90, 0x68,
|
|
||||||
0x00, 0x40, 0x40, 0x80, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x20, 0x40, 0x40, 0x40, 0x40, 0x40, 0x20,
|
|
||||||
0x00, 0x40, 0x20, 0x20, 0x20, 0x20, 0x20, 0x40,
|
|
||||||
0x00, 0x00, 0xA8, 0x70, 0xF8, 0x70, 0xA8, 0x00,
|
|
||||||
0x00, 0x00, 0x20, 0x20, 0xF8, 0x20, 0x20, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x40,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0xF0, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x60,
|
|
||||||
0x00, 0x00, 0x08, 0x10, 0x20, 0x40, 0x80, 0x00,
|
|
||||||
0x00, 0x70, 0x88, 0x88, 0x88, 0x88, 0x88, 0x70,
|
|
||||||
0x00, 0x20, 0x60, 0xA0, 0x20, 0x20, 0x20, 0x20,
|
|
||||||
0x00, 0x70, 0x88, 0x08, 0x10, 0x20, 0x40, 0xF8,
|
|
||||||
0x00, 0x70, 0x88, 0x08, 0x10, 0x08, 0x88, 0x70,
|
|
||||||
0x00, 0x30, 0x50, 0x90, 0x90, 0xF8, 0x10, 0x10,
|
|
||||||
0x00, 0xF8, 0x80, 0x80, 0xF8, 0x08, 0x88, 0x70,
|
|
||||||
0x00, 0x38, 0x40, 0x80, 0xF0, 0x88, 0x88, 0x70,
|
|
||||||
0x00, 0xF8, 0x08, 0x10, 0x20, 0x40, 0x40, 0x40,
|
|
||||||
0x00, 0x70, 0x88, 0x88, 0x70, 0x88, 0x88, 0x70,
|
|
||||||
0x00, 0x70, 0x88, 0x88, 0x78, 0x08, 0x10, 0x60,
|
|
||||||
0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x60, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x60, 0x40,
|
|
||||||
0x00, 0x10, 0x20, 0x40, 0x80, 0x40, 0x20, 0x10,
|
|
||||||
0x00, 0x00, 0x00, 0xF0, 0x00, 0xF0, 0x00, 0x00,
|
|
||||||
0x00, 0x80, 0x40, 0x20, 0x10, 0x20, 0x40, 0x80,
|
|
||||||
0x00, 0x70, 0x88, 0x08, 0x10, 0x20, 0x00, 0x20,
|
|
||||||
0x00, 0x70, 0x88, 0x88, 0xB8, 0xB0, 0x80, 0x78,
|
|
||||||
0x00, 0x70, 0x88, 0x88, 0xF8, 0x88, 0x88, 0x88,
|
|
||||||
0x00, 0xF0, 0x88, 0x88, 0xF0, 0x88, 0x88, 0xF0,
|
|
||||||
0x00, 0x70, 0x88, 0x80, 0x80, 0x80, 0x88, 0x70,
|
|
||||||
0x00, 0xF0, 0x88, 0x88, 0x88, 0x88, 0x88, 0xF0,
|
|
||||||
0x00, 0xF8, 0x80, 0x80, 0xF0, 0x80, 0x80, 0xF8,
|
|
||||||
0x00, 0xF8, 0x80, 0x80, 0xF0, 0x80, 0x80, 0x80,
|
|
||||||
0x00, 0x70, 0x88, 0x80, 0xB8, 0x88, 0x88, 0x70,
|
|
||||||
0x00, 0x88, 0x88, 0x88, 0xF8, 0x88, 0x88, 0x88,
|
|
||||||
0x00, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
|
|
||||||
0x00, 0x10, 0x10, 0x10, 0x10, 0x10, 0x90, 0x60,
|
|
||||||
0x00, 0x88, 0x90, 0xA0, 0xC0, 0xA0, 0x90, 0x88,
|
|
||||||
0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0xF8,
|
|
||||||
0x00, 0x88, 0xD8, 0xA8, 0x88, 0x88, 0x88, 0x88,
|
|
||||||
0x00, 0x88, 0xC8, 0xE8, 0xB8, 0x98, 0x88, 0x88,
|
|
||||||
0x00, 0x70, 0x88, 0x88, 0x88, 0x88, 0x88, 0x70,
|
|
||||||
0x00, 0xF0, 0x88, 0x88, 0xF0, 0x80, 0x80, 0x80,
|
|
||||||
0x00, 0x70, 0x88, 0x88, 0x88, 0xA8, 0x98, 0x70,
|
|
||||||
0x00, 0xF0, 0x88, 0x88, 0xF0, 0xA0, 0x90, 0x88,
|
|
||||||
0x00, 0x70, 0x88, 0x80, 0x70, 0x08, 0x88, 0x70,
|
|
||||||
0x00, 0xF8, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
|
|
||||||
0x00, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x70,
|
|
||||||
0x00, 0x88, 0x88, 0x88, 0x88, 0x88, 0x50, 0x20,
|
|
||||||
0x00, 0x88, 0x88, 0x88, 0x88, 0x88, 0xA8, 0x50,
|
|
||||||
0x00, 0x88, 0x50, 0x20, 0x20, 0x50, 0x88, 0x88,
|
|
||||||
0x00, 0x88, 0x88, 0x50, 0x20, 0x20, 0x20, 0x20,
|
|
||||||
0x00, 0xF8, 0x08, 0x10, 0x20, 0x40, 0x80, 0xF8,
|
|
||||||
0x00, 0x70, 0x40, 0x40, 0x40, 0x40, 0x40, 0x70,
|
|
||||||
0x00, 0x80, 0xC0, 0x60, 0x30, 0x18, 0x08, 0x00,
|
|
||||||
0x00, 0xE0, 0x20, 0x20, 0x20, 0x20, 0x20, 0xE0,
|
|
||||||
0x00, 0x20, 0x50, 0x88, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8,
|
|
||||||
0x00, 0x60, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x70, 0x08, 0x78, 0x88, 0x78,
|
|
||||||
0x00, 0x80, 0x80, 0xF0, 0x88, 0x88, 0x88, 0xF0,
|
|
||||||
0x00, 0x00, 0x00, 0x70, 0x88, 0x80, 0x88, 0x70,
|
|
||||||
0x00, 0x08, 0x08, 0x78, 0x88, 0x88, 0x88, 0x78,
|
|
||||||
0x00, 0x00, 0x00, 0x70, 0x88, 0xF8, 0x80, 0x78,
|
|
||||||
0x00, 0x30, 0x40, 0x40, 0xE0, 0x40, 0x40, 0x40,
|
|
||||||
0x00, 0x00, 0x00, 0x78, 0x88, 0x78, 0x08, 0x70,
|
|
||||||
0x00, 0x80, 0x80, 0xF0, 0x88, 0x88, 0x88, 0x88,
|
|
||||||
0x00, 0x20, 0x00, 0x20, 0x20, 0x20, 0x20, 0x20,
|
|
||||||
0x00, 0x10, 0x00, 0x10, 0x10, 0x10, 0x90, 0x60,
|
|
||||||
0x00, 0x80, 0x80, 0x90, 0xA0, 0xC0, 0xA0, 0x90,
|
|
||||||
0x00, 0xC0, 0x40, 0x40, 0x40, 0x40, 0x40, 0xE0,
|
|
||||||
0x00, 0x00, 0x00, 0xD0, 0xA8, 0xA8, 0xA8, 0xA8,
|
|
||||||
0x00, 0x00, 0x00, 0xF0, 0x88, 0x88, 0x88, 0x88,
|
|
||||||
0x00, 0x00, 0x00, 0x70, 0x88, 0x88, 0x88, 0x70,
|
|
||||||
0x00, 0x00, 0x00, 0xF0, 0x88, 0xF0, 0x80, 0x80,
|
|
||||||
0x00, 0x00, 0x00, 0x78, 0x88, 0x78, 0x08, 0x38,
|
|
||||||
0x00, 0x00, 0x00, 0x70, 0x48, 0x40, 0x40, 0x40,
|
|
||||||
0x00, 0x00, 0x00, 0x78, 0x80, 0x70, 0x08, 0xF0,
|
|
||||||
0x00, 0x40, 0x40, 0xE0, 0x40, 0x40, 0x40, 0x20,
|
|
||||||
0x00, 0x00, 0x00, 0x88, 0x88, 0x88, 0x88, 0x78,
|
|
||||||
0x00, 0x00, 0x00, 0x88, 0x88, 0x88, 0x50, 0x20,
|
|
||||||
0x00, 0x00, 0x00, 0x88, 0x88, 0x88, 0xA8, 0x50,
|
|
||||||
0x00, 0x00, 0x00, 0x88, 0x50, 0x20, 0x50, 0x88,
|
|
||||||
0x00, 0x00, 0x00, 0x88, 0x88, 0x78, 0x08, 0xF0,
|
|
||||||
0x00, 0x00, 0x00, 0xF8, 0x10, 0x20, 0x40, 0xF8,
|
|
||||||
0x00, 0x30, 0x40, 0x40, 0x80, 0x40, 0x40, 0x30,
|
|
||||||
0x00, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
|
|
||||||
0x00, 0xC0, 0x20, 0x20, 0x10, 0x20, 0x20, 0xC0,
|
|
||||||
0x00, 0x00, 0x68, 0x90, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x70, 0x88, 0x88, 0xF8, 0x88, 0x88, 0x88,
|
|
||||||
0x00, 0xF8, 0x88, 0x80, 0xF0, 0x88, 0x88, 0xF0,
|
|
||||||
0x00, 0xF0, 0x88, 0x88, 0xF0, 0x88, 0x88, 0xF0,
|
|
||||||
0x00, 0xF8, 0x48, 0x40, 0x40, 0x40, 0x40, 0xE0,
|
|
||||||
0x00, 0xF0, 0x50, 0x50, 0x50, 0x50, 0x78, 0x88,
|
|
||||||
0x00, 0xF8, 0x80, 0x80, 0xF0, 0x80, 0x80, 0xF8,
|
|
||||||
0x00, 0xA8, 0xA8, 0x70, 0x20, 0x70, 0xA8, 0xA8,
|
|
||||||
0x00, 0x70, 0x88, 0x08, 0x30, 0x08, 0x88, 0x70,
|
|
||||||
0x00, 0x88, 0x98, 0xA8, 0xA8, 0xA8, 0xC8, 0x88,
|
|
||||||
0x00, 0xA8, 0xC8, 0x98, 0xA8, 0xA8, 0xC8, 0x88,
|
|
||||||
0x00, 0x88, 0x90, 0xA0, 0xC0, 0xA0, 0x90, 0x88,
|
|
||||||
0x00, 0xF8, 0x50, 0x50, 0x50, 0x50, 0x50, 0x90,
|
|
||||||
0x00, 0x88, 0xD8, 0xA8, 0x88, 0x88, 0x88, 0x88,
|
|
||||||
0x00, 0x88, 0x88, 0x88, 0xF8, 0x88, 0x88, 0x88,
|
|
||||||
0x00, 0x70, 0x88, 0x88, 0x88, 0x88, 0x88, 0x70,
|
|
||||||
0x00, 0xF8, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88,
|
|
||||||
0x00, 0xF0, 0x88, 0x88, 0xF0, 0x80, 0x80, 0x80,
|
|
||||||
0x00, 0x70, 0x88, 0x80, 0x80, 0x80, 0x88, 0x70,
|
|
||||||
0x00, 0xF8, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
|
|
||||||
0x00, 0x88, 0x88, 0x88, 0x50, 0x20, 0x40, 0x80,
|
|
||||||
0x00, 0x70, 0x20, 0xF8, 0xA8, 0xF8, 0x20, 0x70,
|
|
||||||
0x00, 0x88, 0x88, 0x50, 0x20, 0x50, 0x88, 0x88,
|
|
||||||
0x00, 0x88, 0x88, 0x88, 0x88, 0x88, 0xF8, 0x08,
|
|
||||||
0x00, 0x88, 0x88, 0x88, 0xF8, 0x08, 0x08, 0x38,
|
|
||||||
0x00, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xF8,
|
|
||||||
0x00, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xF8, 0x08,
|
|
||||||
0x00, 0xC0, 0x40, 0x40, 0x78, 0x48, 0x48, 0x78,
|
|
||||||
0x00, 0x88, 0x88, 0xC8, 0xA8, 0xA8, 0xA8, 0xE8,
|
|
||||||
0x00, 0x80, 0x80, 0xF0, 0x88, 0x88, 0x88, 0xF0,
|
|
||||||
0x00, 0xF0, 0x88, 0x08, 0x78, 0x08, 0x88, 0xF0,
|
|
||||||
0x00, 0x90, 0xA8, 0xA8, 0xE8, 0xA8, 0xA8, 0x90,
|
|
||||||
0x00, 0x78, 0x88, 0x88, 0x78, 0x28, 0x48, 0x88,
|
|
||||||
0x00, 0x00, 0x70, 0x08, 0x78, 0x88, 0x88, 0x78,
|
|
||||||
0x00, 0x00, 0xF8, 0x80, 0xF0, 0x88, 0x88, 0xF0,
|
|
||||||
0x00, 0x00, 0xF0, 0x88, 0xF0, 0x88, 0x88, 0xF0,
|
|
||||||
0x00, 0x00, 0xF8, 0x48, 0x48, 0x40, 0x40, 0xE0,
|
|
||||||
0x00, 0x00, 0x78, 0x50, 0x50, 0x50, 0x70, 0x88,
|
|
||||||
0x00, 0x00, 0x70, 0x88, 0xF8, 0x80, 0x88, 0x70,
|
|
||||||
0x00, 0x00, 0xA8, 0x70, 0x20, 0x70, 0xA8, 0xA8,
|
|
||||||
0x00, 0x00, 0x00, 0xF0, 0x88, 0x30, 0x88, 0x70,
|
|
||||||
0x00, 0x00, 0x88, 0x98, 0xA8, 0xC8, 0x88, 0x88,
|
|
||||||
0x00, 0x50, 0x20, 0x88, 0x98, 0xA8, 0xC8, 0x88,
|
|
||||||
0x00, 0x00, 0x88, 0x90, 0xA0, 0xE0, 0x90, 0x88,
|
|
||||||
0x00, 0x00, 0xF8, 0x48, 0x48, 0x48, 0x48, 0xC8,
|
|
||||||
0x00, 0x00, 0x88, 0xD8, 0xA8, 0x88, 0x88, 0x88,
|
|
||||||
0x00, 0x00, 0x88, 0x88, 0xF8, 0x88, 0x88, 0x88,
|
|
||||||
0x00, 0x00, 0x70, 0x88, 0x88, 0x88, 0x88, 0x70,
|
|
||||||
0x00, 0x00, 0xF8, 0x88, 0x88, 0x88, 0x88, 0x88,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x70, 0x50, 0x70, 0x08, 0x78, 0x88, 0x78,
|
|
||||||
0x00, 0x88, 0x00, 0x70, 0x08, 0x78, 0x88, 0x78,
|
|
||||||
0x00, 0x88, 0x00, 0x70, 0x88, 0x88, 0x88, 0x70,
|
|
||||||
0x00, 0x88, 0x00, 0x88, 0x88, 0x88, 0x88, 0x78,
|
|
||||||
0x00, 0x90, 0x00, 0xF0, 0x80, 0xE0, 0x80, 0xF0,
|
|
||||||
0x00, 0x00, 0x70, 0x48, 0x70, 0x48, 0x48, 0xF0,
|
|
||||||
0x00, 0x88, 0x00, 0x20, 0x20, 0x20, 0x20, 0x20,
|
|
||||||
0x00, 0x88, 0x00, 0x70, 0x88, 0xF8, 0x80, 0x78,
|
|
||||||
0x00, 0x70, 0x50, 0x70, 0x88, 0xF8, 0x88, 0x88,
|
|
||||||
0x00, 0x88, 0x70, 0x88, 0x88, 0xF8, 0x88, 0x88,
|
|
||||||
0x00, 0x88, 0x70, 0x88, 0x88, 0x88, 0x88, 0x70,
|
|
||||||
0x00, 0x88, 0x00, 0x88, 0x88, 0x88, 0x88, 0x78,
|
|
||||||
0x00, 0x88, 0xF8, 0x80, 0xE0, 0x80, 0x80, 0xF8,
|
|
||||||
0x00, 0x40, 0x20, 0x70, 0x88, 0xF8, 0x80, 0x78,
|
|
||||||
0x00, 0x10, 0x20, 0x70, 0x88, 0xF8, 0x80, 0x78,
|
|
||||||
0x00, 0x20, 0x50, 0x70, 0x88, 0xF8, 0x80, 0x78,
|
|
||||||
0x00, 0x40, 0x20, 0x70, 0x08, 0x78, 0x88, 0x78,
|
|
||||||
0x00, 0x20, 0x50, 0x00, 0x20, 0x20, 0x20, 0x20,
|
|
||||||
0x00, 0x20, 0x10, 0x88, 0x88, 0x88, 0x88, 0x78,
|
|
||||||
0x00, 0x00, 0x00, 0x70, 0x80, 0x80, 0x70, 0x20,
|
|
||||||
0x00, 0x70, 0x88, 0x40, 0xF0, 0x40, 0x40, 0xF8,
|
|
||||||
0x00, 0x88, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x88, 0x88, 0x88, 0xF0, 0x80, 0x80,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0xF0, 0x88, 0x88, 0xF0, 0x80, 0x80,
|
|
||||||
0x00, 0x00, 0x70, 0x88, 0x80, 0x80, 0x88, 0x70,
|
|
||||||
0x00, 0x00, 0xF8, 0x20, 0x20, 0x20, 0x20, 0x20,
|
|
||||||
0x00, 0x00, 0x88, 0x88, 0x50, 0x20, 0x40, 0x80,
|
|
||||||
0x00, 0x00, 0x70, 0x20, 0xF8, 0xF8, 0x20, 0x70,
|
|
||||||
0x00, 0x00, 0x88, 0x50, 0x20, 0x50, 0x88, 0x88,
|
|
||||||
0x00, 0x00, 0x88, 0x88, 0x88, 0x88, 0xF8, 0x08,
|
|
||||||
0x00, 0x00, 0x88, 0x88, 0x88, 0xF8, 0x08, 0x38,
|
|
||||||
0x00, 0x00, 0xA8, 0xA8, 0xA8, 0xA8, 0xA8, 0xF8,
|
|
||||||
0x00, 0x00, 0xA8, 0xA8, 0xA8, 0xA8, 0xF8, 0x08,
|
|
||||||
0x00, 0x00, 0xC0, 0x40, 0x70, 0x48, 0x48, 0x70,
|
|
||||||
0x00, 0x00, 0x88, 0x88, 0xC8, 0xA8, 0xA8, 0xE8,
|
|
||||||
0x00, 0x00, 0x80, 0x80, 0xF0, 0x88, 0x88, 0xF0,
|
|
||||||
0x00, 0x00, 0x70, 0x88, 0x38, 0x08, 0x88, 0x70,
|
|
||||||
0x00, 0x00, 0x90, 0xA8, 0xA8, 0xE8, 0xA8, 0x90,
|
|
||||||
0x00, 0x00, 0x38, 0x48, 0x48, 0x38, 0x28, 0x48,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
|
|
||||||
|
|
||||||
Submodule
+1
Submodule uefi-d added at e70959ce8a.
@@ -1,143 +0,0 @@
|
|||||||
//video.c
|
|
||||||
// 08/13/03 Josh Holtrop
|
|
||||||
// Modified: 11/12/03
|
|
||||||
|
|
||||||
|
|
||||||
//Initialized the video mode information block video_mode and allocated double-buffer memory for graphics display
|
|
||||||
void video_init(ModeInfoBlock *mib)
|
|
||||||
{
|
|
||||||
video_mode = *mib;
|
|
||||||
|
|
||||||
switch(video_mode.BitsPerPixel)
|
|
||||||
{
|
|
||||||
case 16:
|
|
||||||
vid_ptr16 = (word *) video_mode.PhysBasePtr;
|
|
||||||
video_psetp = &video_psetp16;
|
|
||||||
break;
|
|
||||||
case 24:
|
|
||||||
vid_ptr24 = (byte *) video_mode.PhysBasePtr;
|
|
||||||
video_psetp = &video_psetp24;
|
|
||||||
break;
|
|
||||||
case 32:
|
|
||||||
video_psetp = &video_psetp32;
|
|
||||||
vid_ptr32 = (dword *) video_mode.PhysBasePtr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//Renders a character using stdfont[] as a bitmask
|
|
||||||
void video_renderChar(int x, int y, int character, dword color)
|
|
||||||
{
|
|
||||||
int charpos = (character & 0xFF) * 8;
|
|
||||||
int row;
|
|
||||||
int col;
|
|
||||||
for (row = 0; row < 8; row++)
|
|
||||||
{
|
|
||||||
for (col = 0; col < 5; col++)
|
|
||||||
{
|
|
||||||
if ((stdfont[charpos+row] >> (col+3)) & 0x01)
|
|
||||||
video_pset(x+(5-col), y+row, color);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//Draws a horizontal line
|
|
||||||
void video_horiz(int y, int x1, int x2, dword color)
|
|
||||||
{
|
|
||||||
if (x1 > x2)
|
|
||||||
{
|
|
||||||
int tmp = x2;
|
|
||||||
x2 = x1;
|
|
||||||
x1 = tmp; //x2 >= x1 now
|
|
||||||
}
|
|
||||||
if (x2 < 0)
|
|
||||||
return;
|
|
||||||
if (x1 > video_mode.XResolution)
|
|
||||||
return;
|
|
||||||
if (x1 < 0)
|
|
||||||
x1 = 0;
|
|
||||||
if (x2 > video_mode.XResolution)
|
|
||||||
x2 = video_mode.XResolution;
|
|
||||||
int pixel = y*video_mode.XResolution+x1;
|
|
||||||
for (; x1 <= x2; x1++)
|
|
||||||
{
|
|
||||||
video_psetp(pixel++, color);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//Draws a vertical line
|
|
||||||
void video_vert(int x, int y1, int y2, dword color)
|
|
||||||
{
|
|
||||||
if (y1 > y2)
|
|
||||||
{
|
|
||||||
int tmp = y2;
|
|
||||||
y2 = y1;
|
|
||||||
y1 = tmp; //y2 >= y1 now
|
|
||||||
}
|
|
||||||
if (y2 < 0)
|
|
||||||
return;
|
|
||||||
if (y1 > video_mode.YResolution)
|
|
||||||
return;
|
|
||||||
if (y1 < 0)
|
|
||||||
y1 = 0;
|
|
||||||
if (y2 > video_mode.YResolution)
|
|
||||||
y2 = video_mode.YResolution;
|
|
||||||
int pixel = y1*video_mode.XResolution+x;
|
|
||||||
for (; y1 <= y2; y1++)
|
|
||||||
{
|
|
||||||
video_psetp(pixel, color);
|
|
||||||
pixel+=video_mode.XResolution;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//Draws a rectangle
|
|
||||||
void video_rect(int x1, int y1, int x2, int y2, dword color)
|
|
||||||
{
|
|
||||||
video_horiz(y1, x1, x2, color);
|
|
||||||
video_horiz(y2, x1, x2, color);
|
|
||||||
video_vert(x1, y1, y2, color);
|
|
||||||
video_vert(x2, y1, y2, color);
|
|
||||||
}
|
|
||||||
|
|
||||||
//Draws a filled rectangle
|
|
||||||
void video_rectf(int x1, int y1, int x2, int y2, dword color)
|
|
||||||
{
|
|
||||||
if (y2 < y1)
|
|
||||||
{
|
|
||||||
int tmp = y2;
|
|
||||||
y2 = y1;
|
|
||||||
y1 = tmp;
|
|
||||||
}
|
|
||||||
for (; y1 <= y2; y1++)
|
|
||||||
video_horiz(y1, x1, x2, color);
|
|
||||||
}
|
|
||||||
|
|
||||||
//Draws a single pixel
|
|
||||||
inline void video_pset(int x, int y, dword color)
|
|
||||||
{
|
|
||||||
if ((x < 0) || (x > video_mode.XResolution) || (y < 0) || (y > video_mode.YResolution))
|
|
||||||
return;
|
|
||||||
video_psetp(y*video_mode.XResolution+x, color);
|
|
||||||
}
|
|
||||||
|
|
||||||
//Draws a pixel at the specified pixel position
|
|
||||||
void video_psetp16(int pixel, dword color)
|
|
||||||
{
|
|
||||||
vid_ptr16[pixel] = ((color&0xFF)>>3) | ((((color>>8)&0xFF)>>2)<<5) | ((((color>>16)&0xFF)>>3)<<11);
|
|
||||||
}
|
|
||||||
|
|
||||||
//Draws a pixel at the specified pixel position
|
|
||||||
void video_psetp24(int pixel, dword color)
|
|
||||||
{
|
|
||||||
vid_ptr24[pixel*3] = color & 0xFF;
|
|
||||||
vid_ptr24[pixel*3+1] = (color>>8) & 0xFF;
|
|
||||||
vid_ptr24[pixel*3+2] = (color>>16) & 0xFF;
|
|
||||||
}
|
|
||||||
|
|
||||||
//Draws a pixel at the specified pixel position
|
|
||||||
void video_psetp32(int pixel, dword color)
|
|
||||||
{
|
|
||||||
vid_ptr32[pixel] = color;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,65 +0,0 @@
|
|||||||
//video.h
|
|
||||||
// 08/18/03 Josh Holtrop
|
|
||||||
// Modified: 11/12/03
|
|
||||||
|
|
||||||
void video_init();
|
|
||||||
void video_horiz(int y, int x1, int x2, dword color);
|
|
||||||
void video_vert(int x, int y1, int y2, dword color);
|
|
||||||
void video_rect(int x1, int y1, int x2, int y2, dword color);
|
|
||||||
void video_rectf(int x1, int y1, int x2, int y2, dword color);
|
|
||||||
inline void video_pset(int x, int y, dword color);
|
|
||||||
void video_psetp16(int pixel, dword color);
|
|
||||||
void video_psetp24(int pixel, dword color);
|
|
||||||
void video_psetp32(int pixel, dword color);
|
|
||||||
void video_renderChar(int x, int y, int character, dword color);
|
|
||||||
|
|
||||||
typedef struct{
|
|
||||||
word ModeAttributes;
|
|
||||||
byte WinAAttributes;
|
|
||||||
byte WinBAttributes;
|
|
||||||
word WinGranularity;
|
|
||||||
word WinSize;
|
|
||||||
word WinASegment;
|
|
||||||
word WinBSegment;
|
|
||||||
dword WinFuncPtr;
|
|
||||||
word BytesPerScanLine;
|
|
||||||
|
|
||||||
word XResolution;
|
|
||||||
word YResolution;
|
|
||||||
byte XCharSize;
|
|
||||||
byte YCharSize;
|
|
||||||
byte NumberOfPlanes;
|
|
||||||
byte BitsPerPixel;
|
|
||||||
byte NumberOfBanks;
|
|
||||||
byte MemoryModel;
|
|
||||||
byte BankSize;
|
|
||||||
byte NumberOfImagePages;
|
|
||||||
byte Reserved1;
|
|
||||||
|
|
||||||
byte RedMaskSize;
|
|
||||||
byte RedFieldPosition;
|
|
||||||
byte GreenMaskSize;
|
|
||||||
byte GreenFieldPosition;
|
|
||||||
byte BlueMaskSize;
|
|
||||||
byte BlueFieldPosition;
|
|
||||||
byte RsvdMaskSize;
|
|
||||||
byte RsvdFieldPosition;
|
|
||||||
byte DirectColorModeInfo;
|
|
||||||
|
|
||||||
dword PhysBasePtr;
|
|
||||||
dword OffScreenMemOffset;
|
|
||||||
word OffScreenMemSize;
|
|
||||||
byte Reserved[206];
|
|
||||||
} ModeInfoBlock;
|
|
||||||
|
|
||||||
ModeInfoBlock video_mode;
|
|
||||||
byte *vid_ptr24;
|
|
||||||
word *vid_ptr16;
|
|
||||||
dword *vid_ptr32;
|
|
||||||
void (*video_psetp)(int, dword); //function pointer to set a pixel
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,182 +0,0 @@
|
|||||||
|
|
||||||
// vmm.c
|
|
||||||
// Author: Josh Holtrop
|
|
||||||
// Date: 09/30/03
|
|
||||||
|
|
||||||
PageDirectory *vmm_PDBR = 0;
|
|
||||||
dword vmm_first_virtual_address = 0;
|
|
||||||
|
|
||||||
|
|
||||||
void vmm_init()
|
|
||||||
{
|
|
||||||
if (!(vmm_PDBR = mm_palloc(1, PID_KERNEL)))
|
|
||||||
{
|
|
||||||
printf("ERROR! COULD NOT ALLOCATE PAGE FOR INITIAL PAGE DIRECTORY!!\n");
|
|
||||||
halt();
|
|
||||||
}
|
|
||||||
vmm_init_pagetable((dword)vmm_PDBR);
|
|
||||||
|
|
||||||
if (mm_totalmem % 4096)
|
|
||||||
vmm_first_virtual_address = mm_totalmem + (4096 - (mm_totalmem % 4096));
|
|
||||||
else
|
|
||||||
vmm_first_virtual_address = mm_totalmem;
|
|
||||||
|
|
||||||
if (vmm_mapn(0, 0, 0x03, mm_totalmem/4096+1))
|
|
||||||
{
|
|
||||||
printf("Could not page in all physical RAM!\n");
|
|
||||||
halt();
|
|
||||||
}
|
|
||||||
//we also need to map in the video framebuffer memory:
|
|
||||||
if (video_mode.PhysBasePtr > 0 && video_mode.BitsPerPixel > 0)
|
|
||||||
{
|
|
||||||
if (vmm_mapn(video_mode.PhysBasePtr, video_mode.PhysBasePtr, 0x03, ((video_mode.BitsPerPixel>>3) * video_mode.XResolution * video_mode.YResolution)/4096+1))
|
|
||||||
{
|
|
||||||
printf("Could not page in video memory at 0x%x!\n", video_mode.PhysBasePtr);
|
|
||||||
halt();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void vmm_enable_paging()
|
|
||||||
{
|
|
||||||
write_cr3((dword)vmm_PDBR);
|
|
||||||
write_cr0(0x80000000|read_cr0());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int vmm_map1(dword virtual, dword physical, dword flags)
|
|
||||||
{
|
|
||||||
if (virtual & 0x00000FFF)
|
|
||||||
return 1; // ERROR 1: address not page-aligned
|
|
||||||
if (physical & 0x00000FFF)
|
|
||||||
return 1; // ERROR 1: address not page-aligned
|
|
||||||
int pde = (virtual & 0xFFC00000) >> 22;
|
|
||||||
int pte = (virtual & 0x003FF000) >> 12;
|
|
||||||
if (!(vmm_PDBR->pageTables[pde] & 0x01))
|
|
||||||
{
|
|
||||||
vmm_PDBR->pageTables[pde] = (dword)mm_palloc(1, PID_KERNEL) | 0x7; //user, r/w, present
|
|
||||||
vmm_init_pagetable(vmm_PDBR->pageTables[pde] & 0xFFFFF000);
|
|
||||||
if (vmm_PDBR->pageTables[pde] == 0x07)
|
|
||||||
return 2; // ERROR 2: page table could not be created
|
|
||||||
}
|
|
||||||
PageTable *ptp = (PageTable *)(vmm_PDBR->pageTables[pde] & 0xFFFFF000);
|
|
||||||
if (ptp->page[pte] & 0x01)
|
|
||||||
return 3; // ERROR 3: page table entry already exists
|
|
||||||
ptp->page[pte] = physical | flags;
|
|
||||||
//invlpg();
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int vmm_mapn(dword virtual, dword physical, dword flags, dword n)
|
|
||||||
{
|
|
||||||
int mapped;
|
|
||||||
int result = 0;
|
|
||||||
dword va = virtual;
|
|
||||||
dword pa = physical;
|
|
||||||
for (mapped = 0; mapped < n; mapped++)
|
|
||||||
{
|
|
||||||
if (result = vmm_map1(va, pa, flags))
|
|
||||||
{
|
|
||||||
if (mapped > 0)
|
|
||||||
vmm_unmapn(virtual, mapped);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
va += 4096;
|
|
||||||
pa += 4096;
|
|
||||||
}
|
|
||||||
//invlpg();
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int vmm_unmap1(dword virtual)
|
|
||||||
{
|
|
||||||
if (virtual & 0x00000FFF)
|
|
||||||
return 1; // ERROR 1: address not page-aligned
|
|
||||||
int pde = (virtual & 0xFFC00000) >> 22;
|
|
||||||
int pte = (virtual & 0x003FF000) >> 12;
|
|
||||||
if (!(vmm_PDBR->pageTables[pde] & 0x01))
|
|
||||||
return 2; // ERROR 2: page table not present
|
|
||||||
PageTable *ptp = (PageTable *)(vmm_PDBR->pageTables[pde] & 0xFFFFF000);
|
|
||||||
ptp->page[pte] = 0;
|
|
||||||
//invlpg();
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int vmm_unmapn(dword virtual, dword n)
|
|
||||||
{
|
|
||||||
int reslt;
|
|
||||||
int i;
|
|
||||||
for (i = 0; i < n; i++)
|
|
||||||
{
|
|
||||||
if (reslt = vmm_unmap1(virtual))
|
|
||||||
return reslt;
|
|
||||||
virtual += 4096;
|
|
||||||
}
|
|
||||||
vmm_walkPageTables();
|
|
||||||
//invlpg();
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void vmm_init_pagetable(dword address)
|
|
||||||
{
|
|
||||||
PageTable *ptp = (PageTable *)address;
|
|
||||||
int n;
|
|
||||||
for (n = 0; n < 1024; n++)
|
|
||||||
ptp->page[n] = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int vmm_walkPageTables()
|
|
||||||
{
|
|
||||||
int pde;
|
|
||||||
int pte;
|
|
||||||
int used_ptes;
|
|
||||||
PageTable *ptp;
|
|
||||||
for (pde = 0; pde < 1024; pde++)
|
|
||||||
{
|
|
||||||
if (vmm_PDBR->pageTables[pde] & 0x01)
|
|
||||||
{
|
|
||||||
used_ptes = 0;
|
|
||||||
ptp = (PageTable *)(vmm_PDBR->pageTables[pde] & 0xFFFFF000);
|
|
||||||
for (pte = 0; pte < 1024; pte++)
|
|
||||||
{
|
|
||||||
if (ptp->page[pte] & 0x01) //page table entry present
|
|
||||||
used_ptes++;
|
|
||||||
}
|
|
||||||
if (!(used_ptes)) //no used pte's -- remove page table & page directory entry
|
|
||||||
{
|
|
||||||
mm_pfree(ptp);
|
|
||||||
vmm_PDBR->pageTables[pde] = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void *malloc(dword bytes)
|
|
||||||
{
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int free(void *ptr)
|
|
||||||
{
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
|
|
||||||
// vmm.h
|
|
||||||
// Author: Josh Holtrop
|
|
||||||
// Date: 09/30/03
|
|
||||||
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
dword page[1024];
|
|
||||||
} PageTable;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
dword pageTables[1024];
|
|
||||||
} PageDirectory;
|
|
||||||
|
|
||||||
|
|
||||||
void vmm_enable_paging();
|
|
||||||
void vmm_init();
|
|
||||||
void *malloc(dword bytes);
|
|
||||||
int free(void *ptr);
|
|
||||||
int vmm_map1(dword virtual, dword physical, dword flags);
|
|
||||||
int vmm_mapn(dword virtual, dword physical, dword flags, dword n);
|
|
||||||
int vmm_unmap1(dword virtual);
|
|
||||||
int vmm_unmapn(dword virtual, dword n);
|
|
||||||
void vmm_init_pagetable(dword address);
|
|
||||||
int vmm_walkPageTables();
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,85 @@
|
|||||||
|
project_name "x86_64-elf-gcc"
|
||||||
|
binutils_version = "2.41"
|
||||||
|
binutils_checksum = "ae9a5789e23459e59606e6714723f2d3ffc31c03174191ef0d015bdf06007450"
|
||||||
|
gcc_version = "13.2.0"
|
||||||
|
gcc_checksum = "e275e76442a6067341a27f04c5c6b83d8613144004c0413528863dc6b5c743da"
|
||||||
|
install_path = File.expand_path("x86_64-elf-gcc")
|
||||||
|
target = "x86_64-elf"
|
||||||
|
path_prepend "#{install_path}/bin"
|
||||||
|
|
||||||
|
configure do
|
||||||
|
check_c_compiler "gcc"
|
||||||
|
check_program "make"
|
||||||
|
check_program "bison"
|
||||||
|
check_program "flex"
|
||||||
|
check_program "texi2any", on_fail: "Install the texinfo package"
|
||||||
|
check_program "wget"
|
||||||
|
check_lib "gmp", on_fail: "Install the libgmp-dev package"
|
||||||
|
check_lib "mpc", on_fail: "Install the libmpc-dev package"
|
||||||
|
check_lib "mpfr", on_fail: "Install the libmpfr-dev package"
|
||||||
|
end
|
||||||
|
|
||||||
|
default do
|
||||||
|
unless Dir.exist?(install_path)
|
||||||
|
puts "Building cross compiler toolchain..."
|
||||||
|
|
||||||
|
# Download archives.
|
||||||
|
puts "Downloading binutils..."
|
||||||
|
download "https://ftp.gnu.org/gnu/binutils/binutils-#{binutils_version}.tar.xz",
|
||||||
|
"#{build_dir}/binutils-#{binutils_version}.tar.xz",
|
||||||
|
sha256sum: binutils_checksum
|
||||||
|
|
||||||
|
puts "Downloading gcc..."
|
||||||
|
download "https://ftp.gnu.org/gnu/gcc/gcc-#{gcc_version}/gcc-#{gcc_version}.tar.xz",
|
||||||
|
"#{build_dir}/gcc-#{gcc_version}.tar.xz",
|
||||||
|
sha256sum: gcc_checksum
|
||||||
|
|
||||||
|
# Extract archives.
|
||||||
|
puts "Extracting binutils..."
|
||||||
|
sh "tar", "xJf", "binutils-#{binutils_version}.tar.xz",
|
||||||
|
chdir: build_dir
|
||||||
|
|
||||||
|
puts "Extracting gcc..."
|
||||||
|
sh "tar", "xJf", "gcc-#{gcc_version}.tar.xz",
|
||||||
|
chdir: build_dir
|
||||||
|
|
||||||
|
# Build binutils.
|
||||||
|
puts "Building binutils..."
|
||||||
|
rm_rf "#{build_dir}/build-binutils"
|
||||||
|
mkdir_p "#{build_dir}/build-binutils"
|
||||||
|
cd "#{build_dir}/build-binutils" do
|
||||||
|
sh %W[../binutils-#{binutils_version}/configure
|
||||||
|
--target=#{target} --prefix=#{install_path} --with-sysroot --disable-nls
|
||||||
|
--disable-werror]
|
||||||
|
sh "make"
|
||||||
|
sh "make install"
|
||||||
|
end
|
||||||
|
|
||||||
|
# Build gcc.
|
||||||
|
puts "Building gcc..."
|
||||||
|
rm_rf "#{build_dir}/build-gcc"
|
||||||
|
mkdir_p "#{build_dir}/build-gcc"
|
||||||
|
cd "#{build_dir}/build-gcc" do
|
||||||
|
sh %W[../gcc-#{gcc_version}/configure
|
||||||
|
--target=#{target} --prefix=#{install_path} --disable-nls
|
||||||
|
--enable-languages=c --without-headers]
|
||||||
|
sh "make all-gcc"
|
||||||
|
sh "make all-target-libgcc"
|
||||||
|
sh "make install-gcc"
|
||||||
|
sh "make install-target-libgcc"
|
||||||
|
end
|
||||||
|
|
||||||
|
# Remove archives and build directories if everything succeeded.
|
||||||
|
puts "Cleaning up..."
|
||||||
|
rm_f "#{build_dir}/binutils-#{binutils_version}.tar.xz"
|
||||||
|
rm_rf "#{build_dir}/binutils-#{binutils_version}"
|
||||||
|
rm_rf "#{build_dir}/build-binutils"
|
||||||
|
rm_f "#{build_dir}/gcc-#{gcc_version}.tar.xz"
|
||||||
|
rm_rf "#{build_dir}/gcc-#{gcc_version}"
|
||||||
|
rm_rf "#{build_dir}/build-gcc"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
distclean do
|
||||||
|
rm_rf install_path
|
||||||
|
end
|
||||||
Reference in new issue
Block a user