aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile.config4
-rw-r--r--README.md6
-rw-r--r--devices/i8042.c25
-rw-r--r--devices/mouse.c26
-rw-r--r--devices/pckbd.c16
-rw-r--r--devices/pic_8259.c24
-rw-r--r--devices/uart/uart_16550.c29
-rw-r--r--devices/uart/uart_16550.h56
-rw-r--r--devices/vga.c30
-rw-r--r--devices/vga.h8
-rw-r--r--i686/Makefile2
-rw-r--r--i686/boot.s (renamed from i686/boot.S)37
-rw-r--r--i686/gdt.c6
-rw-r--r--i686/gdt.h72
-rw-r--r--i686/idt.h8
-rw-r--r--i686/isr.c2
-rw-r--r--i686/lgdt.c26
-rw-r--r--i686/lidt.c28
-rw-r--r--i686/macros.s25
-rw-r--r--i686/paging.h86
-rw-r--r--i686/sys/control.h15
-rw-r--r--i686/sys/cpuid.h2
-rw-r--r--i686/sys/io.h50
-rw-r--r--i686/test_gdt.cc1
-rw-r--r--i686/toolchain.mk4
-rw-r--r--lib/blake2/blake2s.c37
-rw-r--r--lib/blake2/blake2s.h24
-rw-r--r--lib/libk/endian.h8
-rw-r--r--lib/libk/endian/little.c4
-rw-r--r--lib/libk/stdio.h22
-rw-r--r--lib/libk/stdio/fprintf.c5
-rw-r--r--lib/libk/stdio/printf.c5
-rw-r--r--lib/libk/stdio/vfprintf.c5
-rw-r--r--lib/libk/stdlib.h10
-rw-r--r--lib/libk/stdlib/linked_list_allocator.c11
-rw-r--r--lib/libk/stdlib/memcpy.c2
-rw-r--r--lib/libk/stdlib/memset.c3
-rw-r--r--lib/libk/string.h13
-rw-r--r--src/boot.h21
-rw-r--r--src/kernel.c9
-rw-r--r--src/mem/vmm.c12
-rw-r--r--src/mmap.c18
-rw-r--r--src/multiboot2.c7
43 files changed, 395 insertions, 409 deletions
diff --git a/Makefile.config b/Makefile.config
index 1f9331d..8ac0c3c 100644
--- a/Makefile.config
+++ b/Makefile.config
@@ -35,10 +35,10 @@ ARCH=i686
# define compiler, linker, archiver and strip and their flags
${ARCH}_AS := i686-elf-as
-${ARCH}_CC := i686-elf-gcc
+${ARCH}_CC := i686-elf-gcc -ansi
${ARCH}_CCID := $(shell ${${ARCH}_CC} --version | head -n1)
${ARCH}_CFLAGS := -Wall -Wextra -Wpedantic -Werror=shadow -Wconversion -fanalyzer \
- -D__ARCH__="${ARCH}" -ffreestanding -std=gnu11 -mgeneral-regs-only \
+ -D__ARCH__="${ARCH}" -ffreestanding -mgeneral-regs-only \
$(shell echo ${CONFIG_CFLAGS})
${ARCH}_LD := i686-elf-ld
diff --git a/README.md b/README.md
index 5323f56..e8adecd 100644
--- a/README.md
+++ b/README.md
@@ -1,5 +1,10 @@
## A simple kernel written in C
+## supported platforms
+- i686
+
+Supported toolchains are defined in $ARCH/toolchain.mk
+
## drivers
- uart: 16550 (write)
- vga text mode
@@ -7,6 +12,7 @@
- ps2 controller: i8042, with keyboard and mouse
## building
+- Code is ANSI C
prerequisites:
* i686-elf-gcc, i686-elf-binutils
* for bootable iso image: grub, mtools
diff --git a/devices/i8042.c b/devices/i8042.c
index c612e6d..4805529 100644
--- a/devices/i8042.c
+++ b/devices/i8042.c
@@ -8,8 +8,8 @@
#include <stdio.h>
#include <sys/io.h>
-// r status register
-// w command register
+/* r status register */
+/* w command register */
#define comm_port 0x64
#define comm_enable_first_ps2 0xae
#define comm_enable_second_ps2 0xa8
@@ -19,13 +19,15 @@
#define data_port 0x60
#define data_enable_scanning 0xf4
-// TODO: All output to port 0x60 or 0x64 must be preceded by waiting for bit 1 (value=2) of port 0x64 to become clear.
-// TODO: Similarly, bytes cannot be read from port 0x60 until bit 0 (value=1) of port 0x64 is set.
+/* TODO: All output to port 0x60 or 0x64 must be preceded by waiting for bit 1 (value=2) of port 0x64 to become clear.
+ * TODO: Similarly, bytes cannot be read from port 0x60 until bit 0 (value=1) of port 0x64 is set. */
void
ps2_ctrl_init()
{
- // eat all previous keystrikes
+ int i;
+
+ /* eat all previous keystrikes */
while (inb(comm_port) & 0x1) inb(data_port);
uint8_t test;
@@ -39,30 +41,30 @@ ps2_ctrl_init()
test = inb(data_port);
printf("i8042: port2 test 0xa9:%x %s\n", test, test == 0x00 ? "ok" : "failed");
- // printf("8042: init keyboard\n");
+ /* printf("8042: init keyboard\n"); */
outb(comm_enable_first_ps2, comm_port);
outb(comm_enable_second_ps2, comm_port);
- // resets the cpu
- // outb(0xfe, 0x64);
+ /* resets the cpu */
+ /* outb(0xfe, 0x64); */
outb(0xf2, 0x60);
printf("i8042: id port1: ");
while ((inb(0x64) & 0x01) == 0) {}
- for (int i = 0; i < 3; ++i) printf("%x ", inb(0x60));
+ for (i = 0; i < 3; ++i) printf("%x ", inb(0x60));
printf("\n");
outb(0xd4, 0x64);
outb(0xf2, 0x60);
printf("i8042: id port2: ");
while ((inb(0x64) & 0x01) == 0) {}
- for (int i = 0; i < 3; ++i) printf("%x ", inb(0x60));
+ for (i = 0; i < 3; ++i) printf("%x ", inb(0x60));
printf("\n");
outb(comm_read_ctrl_config, comm_port);
uint8_t conf = (inb(data_port) | 1) & ~0x10;
- conf |= 0x22; // mouse
+ conf |= 0x22; /* mouse */
outb(comm_write_ctrl_config, comm_port);
outb(conf, data_port);
@@ -81,7 +83,6 @@ ps2_read_port1()
unsigned char
ps2_read_port2()
{
-
outb(0xd4, 0x64);
return inb(0x60);
}
diff --git a/devices/mouse.c b/devices/mouse.c
index a950e8c..a03388a 100644
--- a/devices/mouse.c
+++ b/devices/mouse.c
@@ -7,29 +7,33 @@
void
mouse_init()
{
- // Sending a command or data byte to the mouse (to port 0x60) must be preceded by sending a 0xD4 byte to port 0x64
- // (with appropriate waits on port 0x64, bit 1, before sending each output byte). Note: this 0xD4 byte does not
- // generate any ACK, from either the keyboard or mouse.
+ int i;
- // enable second ps/2
- // outb(0xa8, 0x64);
+ /* Sending a command or data byte to the mouse (to port 0x60) must be preceded by sending a 0xD4 byte to port 0x64
+ * (with appropriate waits on port 0x64, bit 1, before sending each output byte). Note: this 0xD4 byte does not
+ * generate any ACK, from either the keyboard or mouse. */
- // outb(0x64, 0xd4);
- // outb(0x60, 0xf4);
- // printf("mouse_init: enable streaming(0xf4): %x\n", inb(0x60));
+ /* enable second ps/2 */
+ /* outb(0xa8, 0x64); */
+
+ /* outb(0x64, 0xd4); */
+ /* outb(0x60, 0xf4); */
+ /* printf("mouse_init: enable streaming(0xf4): %x\n", inb(0x60)); */
outb(0xd4, 0x64);
- outb(0xeb, 0x60); // single packet
+ outb(0xeb, 0x60); /* single packet */
printf("mouse_init: single packet 0xeb\n");
while ((inb(0x64) & 0x01) == 0) {}
- for (int i = 0; i < 10; ++i) printf("%x ", inb(0x60));
+ for (i = 0; i < 10; ++i) printf("%x ", inb(0x60));
printf("\n");
}
void
mouse_packet()
{
+ int i;
+
printf("mouse packet: ");
- for (int i = 0; i < 4; ++i) printf("%x ", ps2_read_port2());
+ for (i = 0; i < 4; ++i) printf("%x ", ps2_read_port2());
printf("\n");
}
diff --git a/devices/pckbd.c b/devices/pckbd.c
index d550f91..0e99fe6 100644
--- a/devices/pckbd.c
+++ b/devices/pckbd.c
@@ -26,22 +26,22 @@ ps2_keyboard_irq_handler()
const uint8_t key = ps2_read_port1();
switch (key) {
- case 0x2a: // left shift down
- case 0x36: // right shift down
+ case 0x2a: /* left shift down */
+ case 0x36: /* right shift down */
shift_case = 1;
return;
- case 0xaa: // left shift up
- case 0xb6: // right shift up
+ case 0xaa: /* left shift up */
+ case 0xb6: /* right shift up */
shift_case = 0;
return;
- case 0x5b: // left meta
- case 0x5c: // right meta
+ case 0x5b: /* left meta */
+ case 0x5c: /* right meta */
return;
- case 0x58: // F12
- // vga_clear(VGA_COLOR_LIGHT_BLUE, VGA_COLOR_LIGHT_GREY);
+ case 0x58: /* F12 */
+ /* vga_clear(VGA_COLOR_LIGHT_BLUE, VGA_COLOR_LIGHT_GREY); */
return;
}
diff --git a/devices/pic_8259.c b/devices/pic_8259.c
index 75a2d0e..7b4807c 100644
--- a/devices/pic_8259.c
+++ b/devices/pic_8259.c
@@ -6,11 +6,11 @@
#define PIC2 0xa0
#define DATA 1
-// initialization
+/* initialization */
#define ICW1_INIT 0x10
-// TODO
+/* TODO */
#define ICW1_ICW4 0x01
-// 8086/88 mode
+/* 8086/88 mode */
#define ICW4_8086 0x01
void
@@ -19,16 +19,16 @@ pic_init()
outb(ICW1_INIT | ICW1_ICW4, PIC1);
outb(ICW1_INIT | ICW1_ICW4, PIC2);
- outb(0x20, PIC1 + DATA); // offset 0x20
- outb(0x28, PIC2 + DATA); // offset 0x28
+ outb(0x20, PIC1 + DATA); /* offset 0x20 */
+ outb(0x28, PIC2 + DATA); /* offset 0x28 */
- outb(0x04, PIC1 + DATA); // tell master pic there is a slave pic
- outb(0x02, PIC2 + DATA); // tell slave pic its cascade identity
+ outb(0x04, PIC1 + DATA); /* tell master pic there is a slave pic */
+ outb(0x02, PIC2 + DATA); /* tell slave pic its cascade identity */
outb(ICW4_8086, PIC1 + DATA);
outb(ICW4_8086, PIC2 + DATA);
- // PIC masks
+ /* PIC masks */
outb(0xff, PIC1 + DATA);
outb(0xff, PIC2 + DATA);
}
@@ -37,13 +37,13 @@ void
pic_enable()
{
unsigned char mask1 = 0xff;
- mask1 &= ~(1 << 0); // irq0 timer
- mask1 &= ~(1 << 1); // irq1 keyboard
- mask1 &= ~(1 << 2); // irq1 cascade
+ mask1 &= ~(1 << 0); /* irq0 timer */
+ mask1 &= ~(1 << 1); /* irq1 keyboard */
+ mask1 &= ~(1 << 2); /* irq1 cascade */
outb(mask1, PIC1 + DATA);
unsigned char mask2 = 0xff;
- mask2 &= ~(1 << 4); // irq12 mouse
+ mask2 &= ~(1 << 4); /* irq12 mouse */
outb(mask2, PIC2 + DATA);
enable_interrupts();
diff --git a/devices/uart/uart_16550.c b/devices/uart/uart_16550.c
index 47a7fd3..0e19842 100644
--- a/devices/uart/uart_16550.c
+++ b/devices/uart/uart_16550.c
@@ -22,6 +22,7 @@ uart_putc(const FILE *self, char a)
int
uart_puts(const FILE *self, const char *string, int length)
{
+ int i;
int written = 0;
if (length == -1)
@@ -32,7 +33,7 @@ uart_puts(const FILE *self, const char *string, int length)
}
else
- for (int i = 0; i < length; ++i) {
+ for (i = 0; i < length; ++i) {
uart_putc(self, string[i]);
++written;
}
@@ -50,22 +51,22 @@ FILE uart_stream;
FILE *
uart_init(enum UART port)
{
- outb(0x00, port + 1); // Disable all interrupts
- outb(0x80, port + 3); // Enable DLAB (set baud rate divisor)
- outb(0x03, port + 0); // Set divisor to 3 (lo byte) 38400 baud
- outb(0x00, port + 1); // (hi byte)
- outb(0x03, port + 3); // 8 bits, no parity, one stop bit
- outb(0xc7, port + 2); // Enable FIFO, clear them, with 14-byte threshold
- outb(0x0b, port + 4); // IRQs enabled, RTS/DSR set
- outb(0x1e, port + 4); // Set in loopback mode, test the serial chip
- outb(0xae, port + 0); // Test serial chip (send byte 0xAE and check if serial
- // returns same byte)
+ outb(0x00, port + 1); /* Disable all interrupts */
+ outb(0x80, port + 3); /* Enable DLAB (set baud rate divisor) */
+ outb(0x03, port + 0); /* Set divisor to 3 (lo byte) 38400 baud */
+ outb(0x00, port + 1); /* (hi byte) */
+ outb(0x03, port + 3); /* 8 bits, no parity, one stop bit */
+ outb(0xc7, port + 2); /* Enable FIFO, clear them, with 14-byte threshold */
+ outb(0x0b, port + 4); /* IRQs enabled, RTS/DSR set */
+ outb(0x1e, port + 4); /* Set in loopback mode, test the serial chip */
+ outb(0xae, port + 0); /* Test serial chip (send byte 0xAE and check if serial */
+ /* returns same byte) */
- // Check if serial is faulty (i.e: not same byte as sent)
+ /* Check if serial is faulty (i.e: not same byte as sent) */
if (inb(port + 0) != 0xae) { return NULL; }
- // If serial is not faulty set it in normal operation mode
- // (not-loopback with IRQs enabled and OUT#1 and OUT#2 bits enabled)
+ /* If serial is not faulty set it in normal operation mode */
+ /* (not-loopback with IRQs enabled and OUT#1 and OUT#2 bits enabled) */
outb(0x0f, port + 4);
uart_stream.id = port;
uart_stream.putc = &uart_putc;
diff --git a/devices/uart/uart_16550.h b/devices/uart/uart_16550.h
index f8e1931..176dea2 100644
--- a/devices/uart/uart_16550.h
+++ b/devices/uart/uart_16550.h
@@ -8,42 +8,42 @@ int uart_puts(const FILE *self, const char *string, int length);
void uart_flush(__attribute__((unused)) const FILE *self);
enum uart_16550_offset {
- Data = 0, // read from receive buffer / write to transmit buffer | BaudDiv_l
- InterruptControl = 1, // interrupt enable | BaudDiv_h
- FifoControl = 2, // interrupt ID and FIFO control
- LineControl = 3, // most significant bit is the DLAB
+ Data = 0, /* read from receive buffer / write to transmit buffer | BaudDiv_l */
+ InterruptControl = 1, /* interrupt enable | BaudDiv_h */
+ FifoControl = 2, /* interrupt ID and FIFO control */
+ LineControl = 3, /* most significant bit is the DLAB */
ModemControl = 4,
LineStatus = 5,
ModemStatus = 6,
Scratch = 7,
};
-// Line Control
-// | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
-// |dla| | parity | s | data |
+/* Line Control
+ * | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
+ * |dla| | parity | s | data | */
enum LineControl {
- d5bit = 0x00, // 0000 0000 data bits
- d6bit = 0x01, // 0000 0001
- d7bit = 0x02, // 0000 0010
- d8bit = 0x03, // 0000 0011
- // none = 0b00000000, // parity bits
- odd = 0x08, // 0000 1000
- even = 0x18, // 0001 1000
- mark = 0x28, // 0010 1000
- space = 0x38, // 0011 1000
- // s1bit = 0b00000000, // stop bits
- s2bit = 0x04, // 0000 0100 1.5 for 5bit data; 2 otherwise
- dlab = 0x80 // 1000 0000 divisor latch access bit
+ d5bit = 0x00, /* 0000 0000 data bits */
+ d6bit = 0x01, /* 0000 0001 */
+ d7bit = 0x02, /* 0000 0010 */
+ d8bit = 0x03, /* 0000 0011 */
+ /* none = 0b00000000, // parity bits */
+ odd = 0x08, /* 0000 1000 */
+ even = 0x18, /* 0001 1000 */
+ mark = 0x28, /* 0010 1000 */
+ space = 0x38, /* 0011 1000 */
+ /* s1bit = 0b00000000, // stop bits */
+ s2bit = 0x04, /* 0000 0100 1.5 for 5bit data; 2 otherwise */
+ dlab = 0x80 /* 1000 0000 divisor latch access bit */
};
-// Line Status Register
+/* Line Status Register */
enum LineStatus {
- DR = (1 << 0), // data ready: see if there is data to read
- OE = (1 << 1), // overrun error: see if there has been data lost
- PE = (1 << 2), // parity error: see if there was error in transmission
- FE = (1 << 3), // framing error: see if a stop bit was missing
- BI = (1 << 4), // break indicator: see if there is a break in data input
- THRE = (1 << 5), // transmitter holding register empty: see if transmission buffer is empty
- TEMT = (1 << 6), // transmitter empty: see if transmitter is not doing anything
- ERRO = (1 << 7), // impending error: see if there is an error with a word in the input buffer
+ DR = (1 << 0), /* data ready: see if there is data to read */
+ OE = (1 << 1), /* overrun error: see if there has been data lost */
+ PE = (1 << 2), /* parity error: see if there was error in transmission */
+ FE = (1 << 3), /* framing error: see if a stop bit was missing */
+ BI = (1 << 4), /* break indicator: see if there is a break in data input */
+ THRE = (1 << 5), /* transmitter holding register empty: see if transmission buffer is empty */
+ TEMT = (1 << 6), /* transmitter empty: see if transmitter is not doing anything */
+ ERRO = (1 << 7), /* impending error: see if there is an error with a word in the input buffer */
};
diff --git a/devices/vga.c b/devices/vga.c
index e4831b8..b2a6da4 100644
--- a/devices/vga.c
+++ b/devices/vga.c
@@ -16,7 +16,7 @@ struct __attribute__((packed)) VGAEntry {
uint8_t foreground : 4;
uint8_t background : 4;
};
-_Static_assert(sizeof(struct VGAEntry) == 2, "sizeof VGAEntry");
+/* TODO _Static_assert(sizeof(struct VGAEntry) == 2, "sizeof VGAEntry"); */
const int width = 80;
const int height = 25;
@@ -25,7 +25,7 @@ struct VGAEntry *buffer;
int col = 0;
int row = 0;
-// *** Cursor ***
+/* *** Cursor *** */
void
vga_enable_cursor(unsigned char start, unsigned char end)
{
@@ -55,10 +55,12 @@ vga_update_cursor(void)
outb((pos >> 8) & 0xff, cga_dat_port);
}
-// *** Text Mode Output ***
+/* *** Text Mode Output *** */
void
vga_putc(__attribute__((unused)) const FILE *self, char a)
{
+ int i, x, y;
+
switch (a) {
case '\n':
col = 0;
@@ -82,15 +84,15 @@ vga_putc(__attribute__((unused)) const FILE *self, char a)
}
if (row == height) {
- // scroll up
- for (int y = 1; y < height; ++y)
- for (int x = 0; x < width; ++x) {
+ /* scroll up */
+ for (y = 1; y < height; ++y)
+ for (x = 0; x < width; ++x) {
const int prev = (y - 1) * width + x;
const int curr = y * width + x;
buffer[prev] = buffer[curr];
}
- // blank out last row
- for (int i = (height - 1) * width; i < height * width; ++i) buffer[i].text = ' ';
+ /* blank out last row */
+ for (i = (height - 1) * width; i < height * width; ++i) buffer[i].text = ' ';
--row;
}
}
@@ -98,6 +100,8 @@ vga_putc(__attribute__((unused)) const FILE *self, char a)
int
vga_puts(const FILE *self, const char *string, int len)
{
+ int i;
+
int written = 0;
if (len == -1)
while (*string != '\0') {
@@ -107,7 +111,7 @@ vga_puts(const FILE *self, const char *string, int len)
}
else
- for (int i = 0; i < len; ++i) {
+ for (i = 0; i < len; ++i) {
vga_putc(self, string[i]);
++written;
}
@@ -120,7 +124,7 @@ vga_flush(__attribute__((unused)) const FILE *self)
vga_update_cursor();
}
-// *** Text Mode ***
+/* *** Text Mode *** */
FILE vga_stream;
FILE *
@@ -140,8 +144,10 @@ vga_init(void *addr)
void
vga_clear(enum vga_color foreground, enum vga_color background)
{
- for (int y = 0; y < height; ++y)
- for (int x = 0; x < width; ++x) {
+ int x, y;
+
+ for (y = 0; y < height; ++y)
+ for (x = 0; x < width; ++x) {
const int index = y * width + x;
buffer[index].text = ' ';
buffer[index].foreground = foreground;
diff --git a/devices/vga.h b/devices/vga.h
index bd77e2a..cd3a272 100644
--- a/devices/vga.h
+++ b/devices/vga.h
@@ -25,9 +25,9 @@ enum vga_color {
FILE *vga_init(void *addr);
void vga_clear(enum vga_color foreground, enum vga_color background);
-// void vga_putc(char a);
-// void vga_puts(const char *string, int len);
+/* void vga_putc(char a); */
+/* void vga_puts(const char *string, int len); */
-// void vga_enable_cursor(unsigned char start, unsigned char end);
-// void vga_disable_cursor();
+/* void vga_enable_cursor(unsigned char start, unsigned char end); */
+/* void vga_disable_cursor(); */
void vga_update_cursor(void);
diff --git a/i686/Makefile b/i686/Makefile
index 93f9a63..3702bef 100644
--- a/i686/Makefile
+++ b/i686/Makefile
@@ -5,7 +5,7 @@ ${ARCH}_CFLAGS += ${INCLUDES}
${ARCH}_CXXFLAGS += ${INCLUDES}
TARGETLIB += arch
-arch.SRCS = boot.S init.s \
+arch.SRCS = boot.s init.s \
gdt.c lgdt.c \
lidt.c isr.c
diff --git a/i686/boot.S b/i686/boot.s
index 1eea9a3..40b0389 100644
--- a/i686/boot.S
+++ b/i686/boot.s
@@ -1,6 +1,37 @@
-#define ASM_FILE
-#include <multiboot2.h>
-#include "macros.s"
+/* The magic field should contain this. */
+.set MULTIBOOT2_HEADER_MAGIC, 0xe85250d6
+/* This should be in %eax. */
+.set MULTIBOOT2_BOOTLOADER_MAGIC, 0x36d76289
+.set MULTIBOOT_ARCHITECTURE_I386, 0
+.set MULTIBOOT_HEADER_TAG_END, 0
+.set MULTIBOOT_HEADER_TAG_INFORMATION_REQUEST, 1
+.set MULTIBOOT_TAG_TYPE_MMAP, 6
+
+.set PAGE_RO, 0x001
+.set PAGE_RW, 0x003
+
+/* write section to page table macro
+ *
+ * Registers used:
+ * %ecx: loop counter [ set to $1024 ]
+ * %edx: temporary
+ * %esi: current page being mapped
+ * %edi: page entry [ set to $page_addr ]
+ */
+.macro mmap_section begin, end, access
+ mov $\begin, %esi # from $begin
+1: cmpl $\end, %esi # until $end
+ jge 2f
+
+ movl %esi, %edx
+ orl $\access, %edx
+ movl %edx, (%edi)
+
+ addl $4096, %esi # move to next page
+ addl $4, %edi # size of page entry is 4 bytes
+ loop 1b # loop according to %ecx
+2:
+.endm
/* Declare a multiboot header that marks this program as a kernel */
.section .multiboot.header, "a"
diff --git a/i686/gdt.c b/i686/gdt.c
index c0898f3..3148096 100644
--- a/i686/gdt.c
+++ b/i686/gdt.c
@@ -4,11 +4,11 @@ void
SegmentDescriptor(struct SegmentDescriptor_t *self, unsigned base, unsigned limit, uint8_t access)
{
self->base_15_0 = base & 0xffff;
- self->base_23_16 = (base >> 16) & 0xff;
- self->base_31_24 = (base >> 24) & 0xff;
+ self->base_23_16 = (uint8_t)(base >> 16);
+ self->base_31_24 = (uint8_t)(base >> 24);
self->limit_15_0 = (limit <= 0xffff) ? (limit & 0xffff) : ((limit >> 12) & 0xffff);
- self->limit_19_16 = (limit <= 0xffff) ? 0 : ((limit >> 28) & 0xf);
+ self->limit_19_16 = 0xfu & (uint8_t)((limit <= 0xffff) ? 0 : (limit >> 28));
self->access = access;
diff --git a/i686/gdt.h b/i686/gdt.h
index 91de365..2bdfb22 100644
--- a/i686/gdt.h
+++ b/i686/gdt.h
@@ -6,44 +6,44 @@
enum Ring { Ring0 = 0x0, Ring1 = 0x1, Ring2 = 0x2, Ring3 = 0x3 };
struct __attribute__((packed)) Access {
- bool accessed : 1; // if 0, is set by processor when accessed
- bool readwrite : 1; // code seg: read toggle; data seg: write toggle
- bool direction : 1; // code seg: conforming bit; data seg: direction bit
- bool executable : 1; // executable bit
- bool segment : 1; // true for code/data; false for gates/tss
- enum Ring privilege : 2; // descriptor privilege level
- bool present : 1; // true for every active segment
+ unsigned accessed : 1; /* if 0, is set by processor when accessed */
+ unsigned readwrite : 1; /* code seg: read toggle; data seg: write toggle */
+ unsigned direction : 1; /* code seg: conforming bit; data seg: direction bit */
+ unsigned executable : 1; /* executable bit */
+ unsigned segment : 1; /* true for code/data; false for gates/tss */
+ unsigned privilege : 2; /* descriptor privilege level */
+ unsigned present : 1; /* true for every active segment */
};
-_Static_assert(sizeof(struct Access) == 1, "access byte size");
-
-static const struct Access null_access = {false, false, false, false, false, Ring0, false};
-static const struct Access ktext_access = {.readwrite = true, .executable = true, .segment = true, .present = true};
-static const struct Access kdata_access = {.readwrite = true, .segment = true, .present = true};
-
-// Segment Descriptor
-// A memory structure (part of a table) that tells the CPU the attributes of a given segment
-// |31| | | | | | |24|23|22|21|20|19| | |16|15| | | | | | | 8| 7| | | | | | | 0|
-// | base_31_24 | G|DB| | A| lim_19_16 | access | base_23_16 |
-// | base_15_0 | limit_15_0 |
-// |31| | | | | | | | | | | | | | |16|15| | | | | | | | | | | | | | | 0|
-// limit size of segment - 1, either in bytes or in 4KiB chunks (check flags)
-// base address of segment
-// access
-// flags defines the segment chunks and 16/32 bit
+/* _Static_assert(sizeof(struct Access) == 1, "access byte size"); */
+
+static const struct Access null_access = {0, 0, 0, 0, 0, Ring0, 0};
+static const struct Access ktext_access = {0, 1, 0, 1, 1, Ring0, 1};
+static const struct Access kdata_access = {0, 1, 0, 0, 1, Ring0, 1};
+
+/* Segment Descriptor
+ * A memory structure (part of a table) that tells the CPU the attributes of a given segment
+ * |31| | | | | | |24|23|22|21|20|19| | |16|15| | | | | | | 8| 7| | | | | | | 0|
+ * | base_31_24 | G|DB| | A| lim_19_16 | access | base_23_16 |
+ * | base_15_0 | limit_15_0 |
+ * |31| | | | | | | | | | | | | | |16|15| | | | | | | | | | | | | | | 0|
+ * limit size of segment - 1, either in bytes or in 4KiB chunks (check flags)
+ * base address of segment
+ * access
+ * flags defines the segment chunks and 16/32 bit */
struct __attribute__((packed)) SegmentDescriptor_t {
- uint16_t limit_15_0; // low bits of segment limit
- uint16_t base_15_0; // low bits of segment base address
- uint8_t base_23_16; // middle bits of segment base address
- uint8_t access; // access byte
- uint8_t limit_19_16 : 4; // high bits of segment limit
- // flags
- bool a : 1; // unused, available for software use
- bool rsv : 1; // reserved
- bool db : 1; // false => 16-bit seg; true => 32-bit seg
- bool granularity : 1; // limit scaled by 4k when set
- uint8_t base_31_24; // high bits of segment address
+ uint16_t limit_15_0; /* low bits of segment limit */
+ uint16_t base_15_0; /* low bits of segment base address */
+ uint8_t base_23_16; /* middle bits of segment base address */
+ uint8_t access; /* access byte */
+ unsigned limit_19_16 : 4; /* high bits of segment limit */
+ /* flags */
+ bool a : 1; /* unused, available for software use */
+ bool rsv : 1; /* reserved */
+ bool db : 1; /* false => 16-bit seg; true => 32-bit seg */
+ bool granularity : 1; /* limit scaled by 4k when set */
+ uint8_t base_31_24; /* high bits of segment address */
};
-_Static_assert(sizeof(struct SegmentDescriptor_t) == 8, "segment descriptor size");
+/* _Static_assert(sizeof(struct SegmentDescriptor_t) == 8, "segment descriptor size"); */
void SegmentDescriptor(struct SegmentDescriptor_t *self, unsigned base, unsigned limit, uint8_t access);
@@ -51,5 +51,5 @@ void gdt_install();
enum SegmentIndex {
ktextDescriptor = 2 * sizeof(struct SegmentDescriptor_t),
- kdataDescriptor = 3 * sizeof(struct SegmentDescriptor_t),
+ kdataDescriptor = 3 * sizeof(struct SegmentDescriptor_t)
};
diff --git a/i686/idt.h b/i686/idt.h
index 45744dc..ca39bde 100644
--- a/i686/idt.h
+++ b/i686/idt.h
@@ -10,14 +10,14 @@ struct interrupt_frame {
uint32_t ss;
};
-// typedef void (*irq_handler)();
+/* typedef void (*irq_handler)(); */
/* isr.c */
void abort_handler(struct interrupt_frame *frame);
void syscall_handler(struct interrupt_frame *frame);
-void irq0x00(struct interrupt_frame *frame); // timer interrupt
-void irq0x01(struct interrupt_frame *frame); // keyboard interrupt
-void irq0x0c(struct interrupt_frame *frame); // mouse interrupt
+void irq0x00(struct interrupt_frame *frame); /* timer interrupt */
+void irq0x01(struct interrupt_frame *frame); /* keyboard interrupt */
+void irq0x0c(struct interrupt_frame *frame); /* mouse interrupt */
/* lidt.c */
void idt_install();
diff --git a/i686/isr.c b/i686/isr.c
index 8af5d33..acc2961 100644
--- a/i686/isr.c
+++ b/i686/isr.c
@@ -20,7 +20,7 @@ __attribute__((interrupt)) void
syscall_handler(__attribute__((unused)) struct interrupt_frame *frame)
{
unsigned int n;
- asm volatile("mov %%eax, %0" : "=r"(n));
+ __asm__("mov %%eax, %0" : "=r"(n));
printf("syscall %x\n, n");
abort();
}
diff --git a/i686/lgdt.c b/i686/lgdt.c
index 10781db..d1b24c3 100644
--- a/i686/lgdt.c
+++ b/i686/lgdt.c
@@ -10,26 +10,16 @@ static struct SegmentDescriptor_t segments[8] __attribute__((aligned(32)));
void
gdt_install()
{
- SegmentDescriptor(&segments[0], 0, 0, 0); // null segment
- SegmentDescriptor(&segments[2], 0, 0xffffffff, 0x9a); // ktext
- SegmentDescriptor(&segments[3], 0, 0xffffffff, 0x92); // kdata
+ SegmentDescriptor(&segments[0], 0, 0, 0); /* null segment */
+ SegmentDescriptor(&segments[2], 0, 0xffffffff, 0x9a); /* ktext segment */
+ SegmentDescriptor(&segments[3], 0, 0xffffffff, 0x92); /* kdata segment */
const struct Pointer ptr = {.limit = sizeof(segments) - 1, .base = (unsigned)&segments};
- asm volatile("lgdt (%0)" : : "a"(&ptr));
+ __asm__("lgdt (%0)" : : "a"(&ptr));
- // load the kernel data segment
- asm volatile(R"(mov %0, %%ds
- mov %0, %%es
- mov %0, %%fs
- mov %0, %%gs
- mov %0, %%ss
-)"
- :
- : "ax"(kdataDescriptor));
+ /* load the kernel data segment */
+ __asm__("mov %0, %%ds; mov %0, %%es; mov %0, %%fs; mov %0, %%gs; mov %0, %%ss" : : "ax"(kdataDescriptor));
- // load the kernel code segment
- asm volatile(R"(ljmp %0, $1f
- 1:)"
- :
- : "i"(ktextDescriptor));
+ /* load the kernel code segment */
+ __asm__("ljmp %0, $1f\n1:" : : "i"(ktextDescriptor));
}
diff --git a/i686/lidt.c b/i686/lidt.c
index cf8084e..3260eb9 100644
--- a/i686/lidt.c
+++ b/i686/lidt.c
@@ -8,17 +8,17 @@ struct __attribute__((packed)) Pointer {
enum Type {
Null = 0,
- Intr = 0x8e, // 1000 1110 32-bit interrupt
+ Intr = 0x8e, /* 1000 1110 32-bit interrupt */
};
struct __attribute__((packed)) Gate_t {
- uint16_t offset_15_0; // segment offset low
- uint16_t selector; // code segment selector
- uint8_t __unused; // unused in protected mode
- uint8_t type; // interrupt type
- uint16_t offset_31_16; // segment offset high
+ uint16_t offset_15_0; /* segment offset low */
+ uint16_t selector; /* code segment selector */
+ uint8_t __unused; /* unused in protected mode */
+ uint8_t type; /* interrupt type */
+ uint16_t offset_31_16; /* segment offset high */
};
-_Static_assert(sizeof(struct Gate_t) == 8, "interrupt gate size");
+/* _Static_assert(sizeof(struct Gate_t) == 8, "interrupt gate size"); */
void
Gate(struct Gate_t *entry, void (*f)(struct interrupt_frame *), uint16_t selector)
@@ -36,18 +36,20 @@ static struct Gate_t interrupt_table[256] __attribute((aligned(4096)));
void
idt_install()
{
- // exceptions 0x00~0x13
- for (int i = 0; i <= 0x13; ++i) Gate(&interrupt_table[i], &abort_handler, 0x10);
+ int i;
- // irq 0x20~0x2f
- for (int i = 0x22; i <= 0x2f; ++i) Gate(&interrupt_table[i], &abort_handler, 0x10);
+ /* exceptions 0x00~0x13 */
+ for (i = 0; i <= 0x13; ++i) Gate(&interrupt_table[i], &abort_handler, 0x10);
+
+ /* irq 0x20~0x2f */
+ for (i = 0x22; i <= 0x2f; ++i) Gate(&interrupt_table[i], &abort_handler, 0x10);
Gate(&interrupt_table[0x20], &irq0x00, 0x10);
Gate(&interrupt_table[0x21], &irq0x01, 0x10);
Gate(&interrupt_table[0x2c], &irq0x0c, 0x10);
- // syscall 0x80
+ /* syscall 0x80 */
Gate(&interrupt_table[0x80], &syscall_handler, 0x10);
const struct Pointer ptr = {.limit = sizeof(interrupt_table) - 1, .base = (unsigned)&interrupt_table};
- asm volatile("lidt (%0)" : : "a"(&ptr));
+ __asm__("lidt (%0)" : : "a"(&ptr));
}
diff --git a/i686/macros.s b/i686/macros.s
deleted file mode 100644
index a9b8b4d..0000000
--- a/i686/macros.s
+++ /dev/null
@@ -1,25 +0,0 @@
-.set PAGE_RO, 0x001
-.set PAGE_RW, 0x003
-/* write section to page table macro
- *
- * Registers used:
- * %ecx: loop counter [ set to $1024 ]
- * %edx: temporary
- * %esi: current page being mapped
- * %edi: page entry [ set to $page_addr ]
- */
-.macro mmap_section begin, end, access
- mov $\begin, %esi # from $begin
-1: cmpl $\end, %esi # until $end
- jge 2f
-
- movl %esi, %edx
- orl $\access, %edx
- movl %edx, (%edi)
-
- addl $4096, %esi # move to next page
- addl $4, %edi # size of page entry is 4 bytes
- loop 1b # loop according to %ecx
-2:
-.endm
-
diff --git a/i686/paging.h b/i686/paging.h
index f9c04a8..f5bfa78 100644
--- a/i686/paging.h
+++ b/i686/paging.h
@@ -1,59 +1,59 @@
#pragma once
-// DirectoryEntry
-// |31| | | | | | | | | | | | | | | | | | | |11| | 9| 8| 7| 6| 5| 4| 3| 2| 1| 0|
-// | page table 4-kb aligned address | avail | G| S| | A| C| W| U| R| P|
+/* DirectoryEntry
+ * |31| | | | | | | | | | | | | | | | | | | |11| | 9| 8| 7| 6| 5| 4| 3| 2| 1| 0|
+ * | page table 4-kb aligned address | avail | G| S| | A| C| W| U| R| P| */
struct __attribute__((packed)) DirectoryEntry {
- unsigned present : 1; // 0: if set, the page is actually in physical memory
- unsigned writeable : 1; // 1: if set, the page is read/write; otherwise the page is read-only
- unsigned user : 1; // 2: if set, then page can be access by all; otherwise only the supervisor can access it
- unsigned writethrough : 1; // 3: if set, write-through caching is enabled; otherwise write-back is enabled instead
- unsigned cachedisable : 1; // 4: if set, the page will not be cached
- unsigned accessed : 1; // 5: set by the CPU when the page is read from or written to
- unsigned dirty : 1; // 6: used to determine whether a page has been written to
- unsigned pagesize : 1; // 7: page size == 0
+ unsigned present : 1; /* 0: if set, the page is actually in physical memory */
+ unsigned writeable : 1; /* 1: if set, the page is read/write; otherwise the page is read-only */
+ unsigned user : 1; /* 2: if set, then page can be access by all; otherwise only the supervisor can access it */
+ unsigned writethrough : 1; /* 3: if set, write-through caching is enabled; otherwise write-back is enabled instead */
+ unsigned cachedisable : 1; /* 4: if set, the page will not be cached */
+ unsigned accessed : 1; /* 5: set by the CPU when the page is read from or written to */
+ unsigned dirty : 1; /* 6: used to determine whether a page has been written to */
+ unsigned pagesize : 1; /* 7: page size == 0 */
unsigned global : 1;
- unsigned int __available__ : 3; // available to the OS
+ unsigned int __available__ : 3; /* available to the OS */
unsigned int address : 20;
};
-_Static_assert(sizeof(struct DirectoryEntry) == 4, "DirectoryEntry size");
+/* TODO _Static_assert(sizeof(struct DirectoryEntry) == 4, "DirectoryEntry size"); */
-// DirectoryEntry4MB
-// |31| | | | | | | | |22|21|20| | | | | | |13|12|11| | 9| 8| 7| 6| 5| 4| 3| 2| 1| 0|
-// | bits 31-22 of address |RS| bits 39-22 of |AT| avail | G|PS| D| A|CD|WT|US|RW| P|
-// | |VD| address
+/* DirectoryEntry4MB
+ * |31| | | | | | | | |22|21|20| | | | | | |13|12|11| | 9| 8| 7| 6| 5| 4| 3| 2| 1| 0|
+ * | bits 31-22 of address |RS| bits 39-22 of |AT| avail | G|PS| D| A|CD|WT|US|RW| P|
+ * | |VD| address */
struct __attribute__((packed)) DirectoryEntry4MB {
- unsigned present : 1; // 0: if set, the page is actually in physical memory
- unsigned writeable : 1; // 1: if set, the page is read/write; otherwise the page is read-only
- unsigned useraccess : 1; // 2: if set, then page can be access by all; otherwise only the supervisor can access it
- unsigned writethrough : 1; // 3: if set, write-through caching is enabled; otherwise write-back is enabled instead
- unsigned cachedisable : 1; // 4: if set, the page will not be cached
- unsigned accessed : 1; // 5: set by the CPU when the page is read from or written to
- unsigned dirty : 1; // 6: used to determine whether a page has been written to
- unsigned pagesize : 1; // 7: page size == 1
- unsigned global : 1; // 8:
- unsigned __available__ : 3; // 11..9 available to the OS
- unsigned pat : 1; // 12: page attribute table
+ unsigned present : 1; /* 0: if set, the page is actually in physical memory */
+ unsigned writeable : 1; /* 1: if set, the page is read/write; otherwise the page is read-only */
+ unsigned useraccess : 1; /* 2: if set, then page can be access by all; otherwise only the supervisor can access it */
+ unsigned writethrough : 1; /* 3: if set, write-through caching is enabled; otherwise write-back is enabled instead */
+ unsigned cachedisable : 1; /* 4: if set, the page will not be cached */
+ unsigned accessed : 1; /* 5: set by the CPU when the page is read from or written to */
+ unsigned dirty : 1; /* 6: used to determine whether a page has been written to */
+ unsigned pagesize : 1; /* 7: page size == 1 */
+ unsigned global : 1; /* 8: */
+ unsigned __available__ : 3; /* 11..9 available to the OS */
+ unsigned pat : 1; /* 12: page attribute table */
unsigned int address_high : 8;
- unsigned rsvd : 1; // 21
+ unsigned rsvd : 1; /* 21 */
unsigned int address_low : 10;
};
-_Static_assert(sizeof(struct DirectoryEntry4MB) == 4, "DirectoryEntry4M size");
+/* TODO _Static_assert(sizeof(struct DirectoryEntry4MB) == 4, "DirectoryEntry4M size"); */
-// TableEntry
-// |31| | | | | | | | | | | | | | | | | | | |11| | 9| 8| 7| 6| 5| 4| 3| 2| 1| 0|
-// | page table 4-kb aligned address | avail | G| | D| A| C| W|US|RW| P|
+/* TableEntry
+ * |31| | | | | | | | | | | | | | | | | | | |11| | 9| 8| 7| 6| 5| 4| 3| 2| 1| 0|
+ * | page table 4-kb aligned address | avail | G| | D| A| C| W|US|RW| P| */
struct __attribute__((packed)) TableEntry {
- unsigned present : 1; // if set, the page is actually in physical memory
- unsigned writeable : 1; // if set, the page is read/write; otherwise the page is read-only
- unsigned user : 1; // if set, then page can be access by all; otherwise only the supervisor can access it
- unsigned writethrough : 1; // if set, write-through caching is enabled; otherwise write-back is enabled instead
- unsigned cachedisable : 1; // if set, the page will not be cached
- unsigned accessed : 1; // set by the CPU when the page is read from or written to
- unsigned dirty : 1; // used to determine whether a page has been written to
- unsigned pat : 1; // page attribute table?
+ unsigned present : 1; /* if set, the page is actually in physical memory */
+ unsigned writeable : 1; /* if set, the page is read/write; otherwise the page is read-only */
+ unsigned user : 1; /* if set, then page can be access by all; otherwise only the supervisor can access it */
+ unsigned writethrough : 1; /* if set, write-through caching is enabled; otherwise write-back is enabled instead */
+ unsigned cachedisable : 1; /* if set, the page will not be cached */
+ unsigned accessed : 1; /* set by the CPU when the page is read from or written to */
+ unsigned dirty : 1; /* used to determine whether a page has been written to */
+ unsigned pat : 1; /* page attribute table? */
unsigned global : 1;
- unsigned int __available__ : 3; // available to the OS
+ unsigned int __available__ : 3; /* available to the OS */
unsigned int address : 20;
};
-_Static_assert(sizeof(struct TableEntry) == 4, "TableEntry size");
+/* TODO _Static_assert(sizeof(struct TableEntry) == 4, "TableEntry size"); */
diff --git a/i686/sys/control.h b/i686/sys/control.h
index 7dde3c8..89ab067 100644
--- a/i686/sys/control.h
+++ b/i686/sys/control.h
@@ -1,25 +1,24 @@
#pragma once
-static inline void
+static __inline__ void
abort()
{
/* Symbol h is already defined?
-asm volatile(R"(cli
+__asm__(R"(cli
h: hlt
jmp h)");
*/
- asm volatile(R"(cli
-hlt)");
+ __asm__("cli; hlt");
}
-static inline void
+static __inline__ void
enable_interrupts()
{
- asm volatile("sti");
+ __asm__("sti");
}
-static inline void
+static __inline__ void
disable_interrupts()
{
- asm volatile("cli");
+ __asm__("cli");
}
diff --git a/i686/sys/cpuid.h b/i686/sys/cpuid.h
index 65b43c6..6613967 100644
--- a/i686/sys/cpuid.h
+++ b/i686/sys/cpuid.h
@@ -12,7 +12,7 @@ struct CPUVersion {
unsigned int family_ex : 8;
unsigned int __unused_2 : 4;
} __attribute__((packed, aligned(__alignof__(unsigned int))));
-// FIXME _Static_assert(sizeof(struct CPUVersion) == sizeof(unsigned int), "cpuid version struct size");
+/* FIXME _Static_assert(sizeof(struct CPUVersion) == sizeof(unsigned int), "cpuid version struct size"); */
unsigned int
family(const struct CPUVersion v)
diff --git a/i686/sys/io.h b/i686/sys/io.h
index da586b9..403dc54 100644
--- a/i686/sys/io.h
+++ b/i686/sys/io.h
@@ -1,6 +1,6 @@
#pragma once
-// port listings
+/* port listings */
enum UART {
COM1 = 0x3f8,
COM2 = 0x2f8,
@@ -12,81 +12,81 @@ enum UART {
COM8 = 0x4e8,
};
-static inline void
+static __inline__ void
outb(unsigned char val, unsigned short port)
{
- asm volatile("outb %0,%1" : : "a"(val), "dN"(port));
+ __asm__("outb %0,%1" : : "a"(val), "dN"(port));
}
-static inline void
+static __inline__ void
outw(unsigned short val, unsigned short port)
{
- asm volatile("outw %0,%1" : : "a"(val), "dN"(port));
+ __asm__("outw %0,%1" : : "a"(val), "dN"(port));
}
-static inline void
+static __inline__ void
outl(unsigned int val, unsigned short port)
{
- asm volatile("outl %0,%1" : : "a"(val), "dN"(port));
+ __asm__("outl %0,%1" : : "a"(val), "dN"(port));
}
-static inline unsigned char
+static __inline__ unsigned char
inb(unsigned short port)
{
unsigned char val;
- asm volatile("inb %1,%0" : "=a"(val) : "dN"(port));
+ __asm__("inb %1,%0" : "=a"(val) : "dN"(port));
return val;
}
-static inline unsigned short
+static __inline__ unsigned short
inw(unsigned short port)
{
unsigned short val;
- asm volatile("inw %1,%0" : "=a"(val) : "dN"(port));
+ __asm__("inw %1,%0" : "=a"(val) : "dN"(port));
return val;
}
-static inline unsigned int
+static __inline__ unsigned int
inl(unsigned short port)
{
unsigned int val;
- asm volatile("inl %1,%0" : "=a"(val) : "dN"(port));
+ __asm__("inl %1,%0" : "=a"(val) : "dN"(port));
return val;
}
-static inline void
+static __inline__ void
outsb(unsigned short port, const void *__buf, unsigned long __n)
{
- asm volatile("cld; rep; outsb" : "+S"(__buf), "+c"(__n) : "d"(port));
+ __asm__("cld; rep; outsb" : "+S"(__buf), "+c"(__n) : "d"(port));
}
-static inline void
+static __inline__ void
outsw(unsigned short port, const void *__buf, unsigned long __n)
{
- asm volatile("cld; rep; outsw" : "+S"(__buf), "+c"(__n) : "d"(port));
+ __asm__("cld; rep; outsw" : "+S"(__buf), "+c"(__n) : "d"(port));
}
-static inline void
+static __inline__ void
outsl(unsigned short port, const void *__buf, unsigned long __n)
{
- asm volatile("cld; rep; outsl" : "+S"(__buf), "+c"(__n) : "d"(port));
+ __asm__("cld; rep; outsl" : "+S"(__buf), "+c"(__n) : "d"(port));
}
-static inline void
+static __inline__ void
insb(unsigned short port, void *__buf, unsigned long __n)
{
- asm volatile("cld; rep; insb" : "+D"(__buf), "+c"(__n) : "d"(port));
+ __asm__("cld; rep; insb" : "+D"(__buf), "+c"(__n) : "d"(port));
}
-static inline void
+static __inline__ void
insw(unsigned short port, void *__buf, unsigned long __n)
{
- asm volatile("cld; rep; insw" : "+D"(__buf), "+c"(__n) : "d"(port));
+ __asm__("cld; rep; insw" : "+D"(__buf), "+c"(__n) : "d"(port));
}
-static inline void
+static __inline__ void
insl(unsigned short port, void *__buf, unsigned long __n)
{
- asm volatile("cld; rep; insl" : "+D"(__buf), "+c"(__n) : "d"(port));
+ __asm__("cld; rep; insl" : "+D"(__buf), "+c"(__n) : "d"(port));
}
diff --git a/i686/test_gdt.cc b/i686/test_gdt.cc
index d596c3a..e437168 100644
--- a/i686/test_gdt.cc
+++ b/i686/test_gdt.cc
@@ -1,6 +1,5 @@
#include <gtest/gtest.h>
-#define _Static_assert static_assert
#include "gdt.c"
#include "gdt.h"
diff --git a/i686/toolchain.mk b/i686/toolchain.mk
index f3f4732..460717e 100644
--- a/i686/toolchain.mk
+++ b/i686/toolchain.mk
@@ -3,10 +3,10 @@ ARCH=i686
# define compiler, linker, archiver and strip and their flags
${ARCH}_AS := i686-elf-as
-${ARCH}_CC := i686-elf-gcc
+${ARCH}_CC := i686-elf-gcc -ansi
${ARCH}_CCID := $(shell ${${ARCH}_CC} --version | head -n1)
${ARCH}_CFLAGS := -Wall -Wextra -Wpedantic -Werror=shadow -Wconversion -fanalyzer \
- -D__ARCH__="${ARCH}" -ffreestanding -std=gnu11 -mgeneral-regs-only \
+ -D__ARCH__="${ARCH}" -ffreestanding -mgeneral-regs-only \
$(shell echo ${CONFIG_CFLAGS})
${ARCH}_LD := i686-elf-ld
diff --git a/lib/blake2/blake2s.c b/lib/blake2/blake2s.c
index b924a1e..9206c89 100644
--- a/lib/blake2/blake2s.c
+++ b/lib/blake2/blake2s.c
@@ -34,14 +34,16 @@ G(uint32_t v[16], unsigned a, unsigned b, unsigned c, unsigned d, uint32_t x, ui
void
F(struct BLAKE2s_ctx *ctx, uint32_t m[16], uint32_t f)
{
- // Initialize local work vector v
+ unsigned i;
+
+ /* Initialize local work vector v */
uint32_t v[16] = {ctx->h[0], ctx->h[1], ctx->h[2], ctx->h[3], ctx->h[4], ctx->h[5],
ctx->h[6], ctx->h[7], IV[0], IV[1], IV[2], IV[3],
IV[4] ^ ctx->t[0], IV[5] ^ ctx->t[1], IV[6], IV[7]};
- if (f) v[14] = ~v[14]; // if last block flag, invert all bits
+ if (f) v[14] = ~v[14]; /* if last block flag, invert all bits */
- // cryptographic mixing
- for (unsigned i = 0; i < 10; ++i) {
+ /* cryptographic mixing */
+ for (i = 0; i < 10; ++i) {
G(v, 0, 4, 8, 12, m[SIGMA[i][0]], m[SIGMA[i][1]]);
G(v, 1, 5, 9, 13, m[SIGMA[i][2]], m[SIGMA[i][3]]);
G(v, 2, 6, 10, 14, m[SIGMA[i][4]], m[SIGMA[i][5]]);
@@ -53,8 +55,8 @@ F(struct BLAKE2s_ctx *ctx, uint32_t m[16], uint32_t f)
G(v, 3, 4, 9, 14, m[SIGMA[i][14]], m[SIGMA[i][15]]);
}
- // xor the two halves
- for (unsigned i = 0; i < 8; ++i) ctx->h[i] ^= (v[i] ^ v[i + 8]);
+ /* xor the two halves */
+ for (i = 0; i < 8; ++i) ctx->h[i] ^= (v[i] ^ v[i + 8]);
}
int
@@ -70,14 +72,14 @@ BLAKE2s_init(struct BLAKE2s_ctx *ctx, uint8_t outlen, const void *key, uint8_t k
ctx->param.fanout = 1;
ctx->param.depth = 1;
- // copy IV into state vector h
+ /* copy IV into state vector h */
memcpy(ctx->h, IV, 32);
- // copy param block 0 onto h[0]
+ /* copy param block 0 onto h[0] */
ctx->h[0] ^= (ctx->param.depth << 24) ^ (ctx->param.fanout << 16) ^ (keylen << 8) ^ outlen;
if (keylen > 0) {
BLAKE2s_update(ctx, key, keylen);
- ctx->c = 64; // at the end
+ ctx->c = 64; /* at the end */
}
return 0;
@@ -88,15 +90,16 @@ BLAKE2s_init(struct BLAKE2s_ctx *ctx, uint8_t outlen, const void *key, uint8_t k
void
BLAKE2s_update(struct BLAKE2s_ctx *ctx, const void *d, size_t dd)
{
- for (unsigned i = 0; i < dd;) {
+ unsigned i, j;
+ for (i = 0; i < dd;) {
- if (ctx->c == 64) { // if block is full, consume block
+ if (ctx->c == 64) { /* if block is full, consume block */
ctx->t[0] += ctx->c;
if (ctx->t[0] < ctx->c) ctx->t[1] += 1;
- ctx->c = 0; // reset counter
+ ctx->c = 0; /* reset counter */
uint32_t *m = (uint32_t *)ctx->b;
- for (unsigned j = 0; j < 16; ++j) m[j] = htole32(m[j]);
+ for (j = 0; j < 16; ++j) m[j] = htole32(m[j]);
F(ctx, m, 0);
}
@@ -110,14 +113,16 @@ BLAKE2s_update(struct BLAKE2s_ctx *ctx, const void *d, size_t dd)
void
BLAKE2s_final(struct BLAKE2s_ctx *ctx, void *out)
{
+ unsigned i;
+
ctx->t[0] += ctx->c;
if (ctx->t[0] < ctx->c) ctx->t[1] += 1;
- for (; ctx->c < 64; ++(ctx->c)) ctx->b[ctx->c] = 0; // fill up block with zeroes
+ for (; ctx->c < 64; ++(ctx->c)) ctx->b[ctx->c] = 0; /* fill up block with zeroes */
uint32_t *m = (uint32_t *)ctx->b;
- for (unsigned i = 0; i < 16; ++i) m[i] = htole32(m[i]);
+ for (i = 0; i < 16; ++i) m[i] = htole32(m[i]);
F(ctx, m, 1);
- for (unsigned i = 0; i < ctx->param.outlen; ++i) ((uint8_t *)out)[i] = (ctx->h[i >> 2] >> (8 * (i & 3))) & 0xff;
+ for (i = 0; i < ctx->param.outlen; ++i) ((uint8_t *)out)[i] = (ctx->h[i >> 2] >> (8 * (i & 3))) & 0xff;
}
diff --git a/lib/blake2/blake2s.h b/lib/blake2/blake2s.h
index 64b4156..ede170c 100644
--- a/lib/blake2/blake2s.h
+++ b/lib/blake2/blake2s.h
@@ -4,8 +4,8 @@
#include <stdint.h>
struct BLAKE2s_param {
- uint8_t outlen; // digest length
- uint8_t keylen; // key length
+ uint8_t outlen; /* digest length */
+ uint8_t keylen; /* key length */
uint8_t fanout;
uint8_t depth;
uint32_t leaf_length;
@@ -18,11 +18,11 @@ struct BLAKE2s_param {
};
struct BLAKE2s_ctx {
- uint8_t b[64]; // input buffer
- size_t c; // pointer for b[]
- uint32_t h[8]; // chained state vector h
- uint32_t t[2]; // total number of bytes
- struct BLAKE2s_param param; // parameter block
+ uint8_t b[64]; /* input buffer */
+ size_t c; /* pointer for b[] */
+ uint32_t h[8]; /* chained state vector h */
+ uint32_t t[2]; /* total number of bytes */
+ struct BLAKE2s_param param; /* parameter block */
};
/**
@@ -41,11 +41,11 @@ int BLAKE2s_init(struct BLAKE2s_ctx *ctx, uint8_t outlen, const void *key, uint8
void BLAKE2s_update(struct BLAKE2s_ctx *ctx, const void *d, size_t dd);
void BLAKE2s_final(struct BLAKE2s_ctx *ctx, void *out);
-// All-in-one convenience function.
-static inline int
-BLAKE2s(void *out, uint8_t outlen, // return buffer for digest
- const void *key, uint8_t keylen, // optional secret key
- const void *in, size_t inlen) // data to be hashed
+/* All-in-one convenience function. */
+static __inline__ int
+BLAKE2s(void *out, uint8_t outlen, /* return buffer for digest */
+ const void *key, uint8_t keylen, /* optional secret key */
+ const void *in, size_t inlen) /* data to be hashed */
{
struct BLAKE2s_ctx ctx;
if (BLAKE2s_init(&ctx, outlen, key, keylen)) return -1;
diff --git a/lib/libk/endian.h b/lib/libk/endian.h
index 70bc5f7..6aa2669 100644
--- a/lib/libk/endian.h
+++ b/lib/libk/endian.h
@@ -1,13 +1,11 @@
-//=====================================================================
-// spdx-license-identifier: ISC
-//=====================================================================
+/* spdx-license-identifier: ISC */
#pragma once
#include <stdint.h>
-// These functions convert the byte encoding of integer values from host byte order to and from little-endian and
-// big-endian byte order
+/* These functions convert the byte encoding of integer values from host byte order to and from little-endian and
+ * big-endian byte order */
uint16_t htole16(uint16_t host_16b);
uint32_t htole32(uint32_t host_32b);
uint64_t htole64(uint64_t host_64b);
diff --git a/lib/libk/endian/little.c b/lib/libk/endian/little.c
index 042bb55..56a20ad 100644
--- a/lib/libk/endian/little.c
+++ b/lib/libk/endian/little.c
@@ -1,6 +1,4 @@
-//=====================================================================
-// spdx-license-identifier: ISC
-//=====================================================================
+/* spdx-license-identifier: ISC */
#include "endian.h"
diff --git a/lib/libk/stdio.h b/lib/libk/stdio.h
index b28eb5e..7a6e663 100644
--- a/lib/libk/stdio.h
+++ b/lib/libk/stdio.h
@@ -2,22 +2,17 @@
#include <stdarg.h>
-///@defgroup libk libk
-///@{
-///@defgroup stdio stdio
-///@{
-
-/// An object type used for streams
+/** An object type used for streams */
typedef struct FILE {
int id;
- /// Function that prints a character to the stream
+ /** Function that prints a character to the stream */
void (*putc)(const struct FILE *, char);
- /// Function that prints a string to the stream
+ /** Function that prints a string to the stream */
int (*puts)(const struct FILE *, const char *, int);
- /// Flush write buffers
+ /** Flush write buffers */
void (*flush)(const struct FILE *);
} FILE;
@@ -33,17 +28,14 @@ extern FILE *stderr;
* Supports ``%s`` (string), ``%d`` (decimal), ``%u`` (unsigned), ``%x`` (hexadecimal)
* @return number of bytes written
*/
-int printf(const char *restrict format, ...);
+int printf(const char *__restrict__ format, ...);
/**
* Write the formatted string to stream; see printf
*/
-int fprintf(FILE *restrict stream, const char *restrict format, ...);
+int fprintf(FILE *__restrict__ stream, const char *__restrict__ format, ...);
/**
* Write the formatted string to stream; see printf
*/
-int vfprintf(FILE *restrict stream, const char *restrict format, va_list ap);
-
-///@}
-///@}
+int vfprintf(FILE *__restrict__ stream, const char *__restrict__ format, va_list ap);
diff --git a/lib/libk/stdio/fprintf.c b/lib/libk/stdio/fprintf.c
index 9a96dc6..c088f54 100644
--- a/lib/libk/stdio/fprintf.c
+++ b/lib/libk/stdio/fprintf.c
@@ -1,11 +1,12 @@
#include <stdio.h>
int
-fprintf(FILE *restrict stream, const char *restrict format, ...)
+fprintf(FILE *__restrict__ stream, const char *__restrict__ format, ...)
{
+ int c = 0;
va_list ap;
va_start(ap, format);
- int c = vfprintf(stream, format, ap);
+ c += vfprintf(stream, format, ap);
va_end(ap);
return c;
}
diff --git a/lib/libk/stdio/printf.c b/lib/libk/stdio/printf.c
index 4efc1ac..4c45593 100644
--- a/lib/libk/stdio/printf.c
+++ b/lib/libk/stdio/printf.c
@@ -1,11 +1,12 @@
#include <stdio.h>
int
-printf(const char *restrict format, ...)
+printf(const char *__restrict__ format, ...)
{
+ int c = 0;
va_list ap;
va_start(ap, format);
- int c = vfprintf(stdout, format, ap);
+ c += vfprintf(stdout, format, ap);
va_end(ap);
return c;
}
diff --git a/lib/libk/stdio/vfprintf.c b/lib/libk/stdio/vfprintf.c
index d24e43e..a48600b 100644
--- a/lib/libk/stdio/vfprintf.c
+++ b/lib/libk/stdio/vfprintf.c
@@ -4,13 +4,14 @@
static char buffer[3 * sizeof(int) + 2];
int
-vfprintf(FILE *restrict stream, const char *restrict format, va_list params)
+vfprintf(FILE *__restrict__ stream, const char *__restrict__ format, va_list params)
{
int written = 0;
+ int i;
int s = 0;
int l = 0;
- for (int i = 0; format[i] != '\0'; ++i) {
+ for (i = 0; format[i] != '\0'; ++i) {
if (format[i] == '%') {
written += stream->puts(stream, &format[s], l);
s = i + 2;
diff --git a/lib/libk/stdlib.h b/lib/libk/stdlib.h
index 84d9b2d..143c931 100644
--- a/lib/libk/stdlib.h
+++ b/lib/libk/stdlib.h
@@ -2,11 +2,6 @@
#include <stddef.h>
-///@defgroup libk libk
-///@{
-///@defgroup stdlib stdlib
-///@{
-
/**
* Allocate size bytes and return a pointer to the allocated memory
*/
@@ -25,7 +20,4 @@ void *memset(void *s, int c, long unsigned n);
/**
* Copy n bytes from memory area src to memory area dest. The memory areas must not overlap.
*/
-void *memcpy(void *restrict dest, const void *restrict src, long unsigned n);
-
-///@}
-///@}
+void *memcpy(void *__restrict__ dest, const void *__restrict__ src, long unsigned n);
diff --git a/lib/libk/stdlib/linked_list_allocator.c b/lib/libk/stdlib/linked_list_allocator.c
index 66c63d1..898fd89 100644
--- a/lib/libk/stdlib/linked_list_allocator.c
+++ b/lib/libk/stdlib/linked_list_allocator.c
@@ -31,13 +31,14 @@ alloc_init(void *mem, size_t size)
void *
malloc(size_t size)
{
+ struct Chunk *iter;
if (begin == NULL) return NULL;
- // find free chunk that is at least (size + sizeof(struct Chunk))
- for (struct Chunk *iter = begin; iter != NULL; iter = iter->next) {
+ /* find free chunk that is at least (size + sizeof(struct Chunk)) */
+ for (iter = begin; iter != NULL; iter = iter->next) {
if (iter->used != 0 || iter->size < size) continue;
- // if there's at least sizeof(struct Chunk) bytes left over, create a new Chunk
+ /* if there's at least sizeof(struct Chunk) bytes left over, create a new Chunk */
if (iter->size >= (size + 2 * sizeof(struct Chunk))) {
struct Chunk *next = (struct Chunk *)((uintptr_t)iter + sizeof(struct Chunk) + size);
Chunk_ctor(next, iter->size - size - sizeof(struct Chunk));
@@ -61,14 +62,14 @@ free(void *ptr)
struct Chunk *chunk = (struct Chunk *)((uintptr_t)ptr - sizeof(struct Chunk));
chunk->used = 0;
- // merge next chunk
+ /* merge next chunk */
if (chunk->next != NULL && chunk->next->used == 0) {
chunk->size += chunk->next->size + sizeof(struct Chunk);
chunk->next = chunk->next->next;
if (chunk->next != NULL) chunk->next->prev = chunk;
}
- // merge into prev chunk
+ /* merge into prev chunk */
if (chunk->prev != NULL && chunk->prev->used == 0) {
chunk->prev->size += chunk->size + sizeof(struct Chunk);
chunk->prev->next = chunk->next;
diff --git a/lib/libk/stdlib/memcpy.c b/lib/libk/stdlib/memcpy.c
index 90470d5..db7d21e 100644
--- a/lib/libk/stdlib/memcpy.c
+++ b/lib/libk/stdlib/memcpy.c
@@ -1,5 +1,5 @@
void *
-memcpy(void *restrict dest, const void *restrict src, long unsigned n)
+memcpy(void *__restrict__ dest, const void *__restrict__ src, long unsigned n)
{
char *pDest = (char *)dest;
const char *pSrc = (const char *)src;
diff --git a/lib/libk/stdlib/memset.c b/lib/libk/stdlib/memset.c
index a16bd05..2a86f8e 100644
--- a/lib/libk/stdlib/memset.c
+++ b/lib/libk/stdlib/memset.c
@@ -1,7 +1,8 @@
void *
memset(void *s, int c, long unsigned n)
{
+ unsigned i;
char *pDest = (char *)s;
- for (unsigned i = 0; i < n; ++i) pDest[i] = (char)c;
+ for (i = 0; i < n; ++i) pDest[i] = (char)c;
return s;
}
diff --git a/lib/libk/string.h b/lib/libk/string.h
index c8196c8..46d8636 100644
--- a/lib/libk/string.h
+++ b/lib/libk/string.h
@@ -1,17 +1,9 @@
#pragma once
-///@defgroup libk libk
-///@{
-///@defgroup string string
-///@{
-
#define OCTAL 8
#define DECIMAL 10
#define HEX 16
-#ifdef __cplusplus
-extern "C" {
-#endif
/**
* Convert int into a string
*/
@@ -20,9 +12,4 @@ char *itoa(char *p, int x, int base);
* Convert unsigned int into a string
*/
char *utoa(char *p, unsigned x, int base);
-#ifdef __cplusplus
-}
-#endif
-///@}
-///@}
diff --git a/src/boot.h b/src/boot.h
index 8d62bdc..646fb4c 100644
--- a/src/boot.h
+++ b/src/boot.h
@@ -1,24 +1,21 @@
-#pragma once
+/* *** glitch kernel ***
+ * spdx-license-identifier: ISC
+ * description: kernel boot information
+ * */
-#ifdef __cplusplus
-extern "C" {
-#endif
+#pragma once
typedef struct {
- // kernel command line
+ /* kernel command line */
char cmdline[64];
- // memory map
+ /* memory map */
unsigned bitmap[1024 * 32];
- // module
+ /* module */
unsigned module_start;
unsigned module_end;
char module_cmdline[64];
} boot_info_t;
-_Static_assert((1024 * 32 * sizeof(unsigned) * 8) == (1024 * 1024), "bitmap size check");
-
-#ifdef __cplusplus
-}
-#endif
+/* TODO _Static_assert((1024 * 32 * sizeof(unsigned) * 8) == (1024 * 1024), "bitmap size check"); */
diff --git a/src/kernel.c b/src/kernel.c
index 3ee231e..8d7560b 100644
--- a/src/kernel.c
+++ b/src/kernel.c
@@ -1,8 +1,7 @@
-//=====================================================================
-// glitch kernel
-// spdx-license-identifier: ISC
-// description: kernel entry point
-//=====================================================================
+/* *** glitch kernel ***
+ * spdx-license-identifier: ISC
+ * description: kernel entry point
+ * */
#include "conf.h"
#include "mem.h"
diff --git a/src/mem/vmm.c b/src/mem/vmm.c
index 77b06a8..f62eadf 100644
--- a/src/mem/vmm.c
+++ b/src/mem/vmm.c
@@ -18,8 +18,8 @@ vmm_map(unsigned int paddr, unsigned int vaddr)
{
if (paddr & 0xfff || vaddr & 0xfff) return 0;
- const unsigned table_idx = vaddr >> 22; // high 10 bits
- const unsigned entry_idx = (vaddr >> 12) & 0x3ff; // low 10 bits
+ const unsigned table_idx = vaddr >> 22; /* high 10 bits */
+ const unsigned entry_idx = (vaddr >> 12) & 0x3ff; /* low 10 bits */
if (k_pagedir[table_idx].present == 0) return 0;
struct TableEntry *table = (struct TableEntry *)to_vaddr(k_pagedir[table_idx].address << 12);
@@ -34,12 +34,8 @@ vmm_map(unsigned int paddr, unsigned int vaddr)
void
alloc4M()
{
- // enable pse in cr4
- asm volatile(R"(
- movl %cr4, %eax
- orl $0x10, %eax
- movl %eax, %cr4
-)");
+ /* enable pse in cr4 */
+ __asm__("movl %cr4, %eax; orl $0x10, %eax; movl %eax, %cr4");
struct DirectoryEntry4MB *directory = (struct DirectoryEntry4MB *)&k_pagedir[0x301];
directory->address_low = 0x1;
diff --git a/src/mmap.c b/src/mmap.c
index 3fe35b5..c28ce79 100644
--- a/src/mmap.c
+++ b/src/mmap.c
@@ -6,15 +6,17 @@
__attribute__((section(".multiboot.text"))) unsigned
multiboot2_mmap(const struct multiboot_mmap_entry entries[], unsigned entry_count, unsigned bitmap[1024 * 32])
{
- // clear out the bitmap
- for (unsigned i = 0; i < 1024 * 32; ++i) bitmap[i] = 0;
+ unsigned i, l;
+
+ /* clear out the bitmap */
+ for (i = 0; i < 1024 * 32; ++i) bitmap[i] = 0;
unsigned avail_frames = 0;
- // loop through all the mmap_entry structures where type is MULTIBOOT_MEMORY_AVAILABLE
- for (unsigned i = 0; i < entry_count; ++i) {
+ /* loop through all the mmap_entry structures where type is MULTIBOOT_MEMORY_AVAILABLE */
+ for (i = 0; i < entry_count; ++i) {
if (entries[i].type != MULTIBOOT_MEMORY_AVAILABLE) continue;
- // number of frames in this entry
+ /* number of frames in this entry */
unsigned n_frames = entries[i].len / 4096;
avail_frames += n_frames;
@@ -23,8 +25,8 @@ multiboot2_mmap(const struct multiboot_mmap_entry entries[], unsigned entry_coun
n_frames / 32, n_frames % 32);
#endif
- // the bitmap is an array of blocks, each holding 32 (2^5) values
- unsigned table_idx = (entries[i].addr >> 17); // get the upper 15 bits
+ /* the bitmap is an array of blocks, each holding 32 (2^5) values */
+ unsigned table_idx = (entries[i].addr >> 17); /* get the upper 15 bits */
while (n_frames != 0) {
if (n_frames >= 32) {
@@ -34,7 +36,7 @@ multiboot2_mmap(const struct multiboot_mmap_entry entries[], unsigned entry_coun
}
else {
unsigned block = bitmap[table_idx];
- for (unsigned l = 0; l < n_frames; ++l) block |= (1 << l);
+ for (l = 0; l < n_frames; ++l) block |= (1 << l);
bitmap[table_idx] = block;
n_frames = 0;
}
diff --git a/src/multiboot2.c b/src/multiboot2.c
index ea06e96..bd6250f 100644
--- a/src/multiboot2.c
+++ b/src/multiboot2.c
@@ -7,7 +7,8 @@ boot_info_t info __attribute__((section(".init")));
__attribute__((section(".multiboot.text"))) void
multiboot_strncpy(char *dest, const char *src, unsigned n)
{
- for (unsigned i = 0; i < n && src[i] != '\0'; ++i) dest[i] = src[i];
+ unsigned i;
+ for (i = 0; i < n && src[i] != '\0'; ++i) dest[i] = src[i];
}
__attribute__((section(".multiboot.text"))) void
@@ -44,6 +45,6 @@ __multiboot2(multiboot_uint32_t addr)
break;
default:
break;
- } // switch
- } // for
+ }
+ }
}