summaryrefslogtreecommitdiff
path: root/kernel
diff options
context:
space:
mode:
authorAnton Kling <anton@kling.gg>2024-02-21 00:14:29 +0100
committerAnton Kling <anton@kling.gg>2024-02-21 00:14:29 +0100
commit8ff63b062d724826d8017504063df9b92f8e6703 (patch)
tree03bf0b5a278a4908da8912f956e5651bea9412f1 /kernel
parenta85eacdd2406fede4d6ff5cb130b1758978cabb3 (diff)
New clang-format options
Diffstat (limited to 'kernel')
-rw-r--r--kernel/Makefile3
-rw-r--r--kernel/arch/i386/mmu.c87
-rw-r--r--kernel/cpu/idt.c26
-rw-r--r--kernel/cpu/syscall.c24
-rw-r--r--kernel/crypto/ChaCha20/chacha20.c6
-rw-r--r--kernel/drivers/ahci.c21
-rw-r--r--kernel/drivers/ata.c15
-rw-r--r--kernel/drivers/keyboard.c8
-rw-r--r--kernel/drivers/mouse.c3
-rw-r--r--kernel/drivers/pci.c15
-rw-r--r--kernel/drivers/rtl8139.c6
-rw-r--r--kernel/elf.c3
-rw-r--r--kernel/fs/devfs.c12
-rw-r--r--kernel/fs/ext2.c84
-rw-r--r--kernel/fs/fifo.c6
-rw-r--r--kernel/fs/shm.c15
-rw-r--r--kernel/fs/tmpfs.c6
-rw-r--r--kernel/fs/vfs.c76
-rw-r--r--kernel/halts.c5
-rw-r--r--kernel/hashmap/hashmap.c65
-rw-r--r--kernel/kmalloc.c45
-rw-r--r--kernel/kubsan.c2
-rw-r--r--kernel/lib/buffered_write.c2
-rw-r--r--kernel/lib/stack.h4
-rw-r--r--kernel/libc/stdio/print.c15
-rw-r--r--kernel/libc/string/copy.c9
-rw-r--r--kernel/libc/string/isequal.c12
-rw-r--r--kernel/libc/string/memcmp.c6
-rw-r--r--kernel/libc/string/memcpy.c12
-rw-r--r--kernel/libc/string/memset.c3
-rw-r--r--kernel/libc/string/strlcpy.c6
-rw-r--r--kernel/network/arp.c12
-rw-r--r--kernel/network/ethernet.c3
-rw-r--r--kernel/network/ipv4.c3
-rw-r--r--kernel/network/tcp.c9
-rw-r--r--kernel/poll.c18
-rw-r--r--kernel/random.c6
-rw-r--r--kernel/sched/scheduler.c111
-rw-r--r--kernel/sched/scheduler.h5
-rw-r--r--kernel/socket.c3
-rw-r--r--kernel/syscalls/isatty.c6
-rw-r--r--kernel/syscalls/recvfrom.c3
-rw-r--r--kernel/syscalls/stat.c3
-rw-r--r--kernel/syscalls/write.c3
44 files changed, 501 insertions, 286 deletions
diff --git a/kernel/Makefile b/kernel/Makefile
index c42c95c..107dcd0 100644
--- a/kernel/Makefile
+++ b/kernel/Makefile
@@ -2,7 +2,8 @@ CC="i686-sb-gcc"
AS="i686-sb-as"
OBJ = arch/i386/boot.o init/kernel.o cpu/gdt.o cpu/reload_gdt.o cpu/idt.o cpu/io.o libc/stdio/print.o drivers/keyboard.o log.o drivers/pit.o libc/string/memcpy.o libc/string/strlen.o libc/string/memcmp.o drivers/ata.o libc/string/memset.o cpu/syscall.o read_eip.o libc/exit/assert.o process.o libc/string/strcpy.o arch/i386/mmu.o kmalloc.o fs/ext2.o fs/vfs.o fs/devfs.o cpu/spinlock.o random.o libc/string/strcmp.o crypto/ChaCha20/chacha20.o crypto/SHA1/sha1.o fs/tmpfs.o libc/string/isequal.o drivers/pst.o halts.o syscalls/ppoll.o syscalls/ftruncate.o kubsan.o syscalls/mmap.o drivers/serial.o syscalls/accept.o syscalls/bind.o syscalls/socket.o socket.o poll.o fs/fifo.o hashmap/hashmap.o fs/shm.o syscalls/shm.o elf.o ksbrk.o sched/scheduler.o syscalls/stat.o libc/string/copy.o libc/string/strncpy.o drivers/mouse.o libc/string/strlcpy.o libc/string/strcat.o drivers/vbe.o syscalls/msleep.o syscalls/uptime.o syscalls/mkdir.o drivers/pci.o drivers/rtl8139.o network/ethernet.o network/arp.o network/bytes.o network/ipv4.o network/udp.o syscalls/recvfrom.o math.o syscalls/sendto.o signal.o syscalls/kill.o syscalls/sigaction.o network/tcp.o drivers/ahci.o crypto/xoshiro256plusplus/xoshiro256plusplus.o syscalls/chdir.o syscalls/getcwd.o syscalls/isatty.o syscalls/randomfill.o syscalls/open.o syscalls/write.o syscalls/pwrite.o ipc.o syscalls/ipc.o syscalls/port.o syscalls/map_frames.o syscalls/virtual_to_physical.o syscalls/install_irq.o arch/i386/interrupts.o cpu/isr.o lib/stack.o lib/buffered_write.o lib/list.o
CFLAGS = -Ofast -fsanitize=vla-bound,shift-exponent,pointer-overflow,shift,signed-integer-overflow,bounds -ggdb -ffreestanding -Wall -Werror -mgeneral-regs-only -Wimplicit-fallthrough -I./libc/include/ -I. -Wno-pointer-sign -DKERNEL
-#LDFLAGS=-flto -Ofast
+#CFLAGS = -O0 -ggdb -ffreestanding -Wall -Werror -mgeneral-regs-only -Wimplicit-fallthrough -I./libc/include/ -I. -Wno-pointer-sign -DKERNEL
+LDFLAGS=-flto -Ofast
LDFLAGS=
INCLUDE=-I./includes/ -I../include/ -I./libc/include/
diff --git a/kernel/arch/i386/mmu.c b/kernel/arch/i386/mmu.c
index f958455..201fa8e 100644
--- a/kernel/arch/i386/mmu.c
+++ b/kernel/arch/i386/mmu.c
@@ -77,14 +77,16 @@ u32 mmu_get_number_of_allocated_frames(void) {
Page *get_page(void *ptr, PageDirectory *directory, int create_new_page,
int set_user) {
uintptr_t address = (uintptr_t)ptr;
- if (!directory)
+ if (!directory) {
directory = get_active_pagedirectory();
+ }
address /= 0x1000;
u32 table_index = address / 1024;
if (!directory->tables[table_index]) {
- if (!create_new_page)
+ if (!create_new_page) {
return 0;
+ }
u32 physical;
directory->tables[table_index] =
@@ -121,8 +123,9 @@ void *next_page(void *ptr) {
}
void *align_page(void *a) {
- if ((uintptr_t)a & (PAGE_SIZE - 1))
+ if ((uintptr_t)a & (PAGE_SIZE - 1)) {
return next_page(a);
+ }
return a;
}
@@ -135,12 +138,15 @@ void flush_tlb(void) {
u32 first_free_frame(void) {
for (u32 i = 1; i < INDEX_FROM_BIT(num_of_frames); i++) {
- if (frames[i] == 0xFFFFFFFF)
+ if (frames[i] == 0xFFFFFFFF) {
continue;
+ }
- for (u32 c = 0; c < 32; c++)
- if (!(frames[i] & ((u32)1 << c)))
+ for (u32 c = 0; c < 32; c++) {
+ if (!(frames[i] & ((u32)1 << c))) {
return i * 32 + c;
+ }
+ }
}
kprintf("ERROR Num frames: %x\n", mmu_get_number_of_allocated_frames());
@@ -192,8 +198,9 @@ PageTable *clone_table(u32 src_index, PageDirectory *src_directory,
// copy data to it.
for (u32 i = 0; i < 1024; i++) {
// Find a unused table
- if (src_directory->tables[i])
+ if (src_directory->tables[i]) {
continue;
+ }
// Link the table to the new table temporarily
src_directory->tables[i] = new_table;
@@ -204,8 +211,9 @@ PageTable *clone_table(u32 src_index, PageDirectory *src_directory,
// For each page in the table copy all the data over.
for (u32 c = 0; c < 1024; c++) {
// Only copy pages that are used.
- if (!src->pages[c].frame || !src->pages[c].present)
+ if (!src->pages[c].frame || !src->pages[c].present) {
continue;
+ }
u32 table_data_pointer = i << 22 | c << 12;
u32 src_data_pointer = src_index << 22 | c << 12;
@@ -241,8 +249,9 @@ PageTable *copy_table(PageTable *src, u32 *physical_address) {
}
PageDirectory *clone_directory(PageDirectory *original) {
- if (!original)
+ if (!original) {
original = get_active_pagedirectory();
+ }
u32 physical_address;
PageDirectory *new_directory =
@@ -267,8 +276,9 @@ PageDirectory *clone_directory(PageDirectory *original) {
}
if (original->tables[i] == kernel_directory->tables[i] || i > 641) {
- if (original->tables[i])
+ if (original->tables[i]) {
assert(kernel_directory->tables[i]);
+ }
new_directory->tables[i] = kernel_directory->tables[i];
new_directory->physical_tables[i] = kernel_directory->physical_tables[i];
continue;
@@ -287,8 +297,9 @@ void mmu_allocate_shared_kernel_region(void *rc, size_t n) {
size_t num_pages = n / PAGE_SIZE;
for (size_t i = 0; i <= num_pages; i++) {
Page *p = get_page((void *)(rc + i * 0x1000), NULL, PAGE_ALLOCATE, 0);
- if (!p->present || !p->frame)
+ if (!p->present || !p->frame) {
allocate_frame(p, 0, 1);
+ }
}
}
@@ -296,8 +307,9 @@ void mmu_remove_virtual_physical_address_mapping(void *ptr, size_t length) {
size_t num_pages = (uintptr_t)align_page((void *)length) / PAGE_SIZE;
for (size_t i = 0; i < num_pages; i++) {
Page *p = get_page(ptr + (i * PAGE_SIZE), NULL, PAGE_NO_ALLOCATE, 0);
- if (!p)
+ if (!p) {
return;
+ }
p->frame = 0;
p->present = 0;
}
@@ -389,12 +401,15 @@ void mmu_free_address_range(void *ptr, size_t length) {
size_t num_pages = (size_t)align_page((void *)length) / PAGE_SIZE;
for (size_t i = 0; i < num_pages; i++, ptr += PAGE_SIZE) {
Page *page = get_page(ptr, NULL, PAGE_NO_ALLOCATE, 0);
- if (!page)
+ if (!page) {
continue;
- if (!page->present)
+ }
+ if (!page->present) {
continue;
- if (!page->frame)
+ }
+ if (!page->frame) {
continue;
+ }
write_to_frame(((u32)page->frame) * 0x1000, 0);
page->present = 0;
page->rw = 0;
@@ -446,8 +461,9 @@ struct PhysVirtMap phys_to_virt_map[256] = {0};
void create_physical_to_virtual_mapping(void *physical, void *virtual,
u32 length) {
for (u16 i = 0; i < 256; i++) {
- if (phys_to_virt_map[i].in_use)
+ if (phys_to_virt_map[i].in_use) {
continue;
+ }
phys_to_virt_map[i].physical = (u32)physical;
phys_to_virt_map[i].virtual = (u32) virtual;
phys_to_virt_map[i].length = length;
@@ -459,13 +475,16 @@ void create_physical_to_virtual_mapping(void *physical, void *virtual,
void *physical_to_virtual(void *address) {
for (u16 i = 0; i < 256; i++) {
- if (!phys_to_virt_map[i].in_use)
+ if (!phys_to_virt_map[i].in_use) {
continue;
+ }
if (phys_to_virt_map[i].physical + phys_to_virt_map[i].length <
- (u32)address)
+ (u32)address) {
continue;
- if (phys_to_virt_map[i].physical > (u32)address)
+ }
+ if (phys_to_virt_map[i].physical > (u32)address) {
continue;
+ }
return (void *)phys_to_virt_map[i].virtual;
}
assert(0);
@@ -473,8 +492,9 @@ void *physical_to_virtual(void *address) {
}
void *virtual_to_physical(void *address, PageDirectory *directory) {
- if (0 == directory)
+ if (0 == directory) {
directory = get_active_pagedirectory();
+ }
Page *p = get_page((void *)address, directory, PAGE_NO_ALLOCATE, 0);
return (void *)((u32)p->frame * 0x1000) + (((uintptr_t)address) & 0xFFF);
}
@@ -520,18 +540,21 @@ void *is_valid_user_c_string(const char *ptr, size_t *size) {
size_t s = 0;
for (; ((u32)ptr - (u32)r) < 0x1000;) {
void *page = (void *)((uintptr_t)ptr & (uintptr_t)(~(PAGE_SIZE - 1)));
- if (!is_valid_userpointer(page, PAGE_SIZE))
+ if (!is_valid_userpointer(page, PAGE_SIZE)) {
return NULL;
+ }
if (!((uintptr_t)ptr & (PAGE_SIZE - 1))) {
ptr++;
s++;
}
- for (; (uintptr_t)ptr & (PAGE_SIZE - 1); ptr++, s++)
+ for (; (uintptr_t)ptr & (PAGE_SIZE - 1); ptr++, s++) {
if (!*ptr) {
- if (size)
+ if (size) {
*size = s;
+ }
return r;
}
+ }
}
// String is too long, something has probably gone wrong.
assert(0);
@@ -543,12 +566,15 @@ void *is_valid_userpointer(const void *ptr, size_t s) {
size_t num_pages = (uintptr_t)align_page((void *)s) / 0x1000;
for (size_t i = 0; i < num_pages; i++, t += 0x1000) {
Page *page = get_page((void *)t, NULL, PAGE_NO_ALLOCATE, 0);
- if (!page)
+ if (!page) {
return NULL;
- if (!page->present)
+ }
+ if (!page->present) {
return NULL;
- if (!page->user)
+ }
+ if (!page->user) {
return NULL;
+ }
}
return (void *)ptr;
}
@@ -565,8 +591,9 @@ void enable_paging(void) {
}
void create_table(int table_index) {
- if (kernel_directory->tables[table_index])
+ if (kernel_directory->tables[table_index]) {
return;
+ }
u32 physical;
kernel_directory->tables[table_index] = (PageTable *)0xDEADBEEF;
kernel_directory->tables[table_index] =
@@ -600,8 +627,9 @@ void paging_init(u64 memsize) {
PageTable *table = kernel_directory->tables[i];
write_to_frame(kernel_directory->physical_tables[i], 1);
for (size_t j = 0; j < 1024; j++) {
- if (!table->pages[j].present)
+ if (!table->pages[j].present) {
continue;
+ }
// Add the frame to our bitmap to ensure it does not get used by
// another newly created page.
write_to_frame(table->pages[j].frame * 0x1000, 1);
@@ -611,8 +639,9 @@ void paging_init(u64 memsize) {
switch_page_directory(kernel_directory);
// Make null dereferences crash.
get_page(NULL, kernel_directory, PAGE_ALLOCATE, 0)->present = 0;
- for (int i = 0; i < 25; i++)
+ for (int i = 0; i < 25; i++) {
create_table(770 + i);
+ }
kernel_directory = clone_directory(kernel_directory);
switch_page_directory(kernel_directory);
diff --git a/kernel/cpu/idt.c b/kernel/cpu/idt.c
index 32186c5..3d866e5 100644
--- a/kernel/cpu/idt.c
+++ b/kernel/cpu/idt.c
@@ -40,8 +40,9 @@ void format_descriptor(u32 offset, u16 code_segment, u8 type_attribute,
}
__attribute__((no_caller_saved_registers)) void EOI(u8 irq) {
- if (irq > 7)
+ if (irq > 7) {
outb(SLAVE_PIC_COMMAND_PORT, 0x20);
+ }
outb(MASTER_PIC_COMMAND_PORT, 0x20);
}
@@ -83,23 +84,27 @@ void page_fault(reg_t *regs) {
kprintf("Error Code: %x\n", regs->error_code);
kprintf("Instruction Pointer: %x\n", regs->eip);
- if (regs->error_code & (1 << 0))
+ if (regs->error_code & (1 << 0)) {
kprintf("page-protection violation\n");
- else
+ } else {
kprintf("non-present page\n");
+ }
- if (regs->error_code & (1 << 1))
+ if (regs->error_code & (1 << 1)) {
kprintf("write access\n");
- else
+ } else {
kprintf("read access\n");
+ }
- if (regs->error_code & (1 << 2))
+ if (regs->error_code & (1 << 2)) {
kprintf("CPL = 3\n");
+ }
- if (regs->error_code & (1 << 4))
+ if (regs->error_code & (1 << 4)) {
kprintf("Attempted instruction fetch\n");
+ }
- dump_backtrace(5);
+ dump_backtrace(12);
asm("hlt");
for (;;)
;
@@ -161,8 +166,9 @@ void IRQ_set_mask(unsigned char IRQline) {
u16 port;
u8 value;
port = (IRQline < 8) ? MASTER_PIC_DATA_PORT : SLAVE_PIC_DATA_PORT;
- if (IRQline >= 8)
+ if (IRQline >= 8) {
IRQline -= 8;
+ }
value = inb(port) | (1 << IRQline);
outb(port, value);
}
@@ -217,7 +223,7 @@ typedef int (*interrupt_handler)(reg_t *);
interrupt_handler list_of_handlers[256];
void int_handler(reg_t *r) {
- interrupt_handler handler = list_of_handlers[r->int_no];
+ const interrupt_handler handler = list_of_handlers[r->int_no];
if (NULL == handler) {
kprintf("[NOTE] Interrupt(0x%x) called but has no interrupt handler",
r->int_no);
diff --git a/kernel/cpu/syscall.c b/kernel/cpu/syscall.c
index 1ed5f41..e2ea7e3 100644
--- a/kernel/cpu/syscall.c
+++ b/kernel/cpu/syscall.c
@@ -24,15 +24,17 @@ int syscall_exec(SYS_EXEC_PARAMS *args) {
}
char **new_argv = kallocarray(argc + 1, sizeof(char *));
- for (int i = 0; i < argc; i++)
+ for (int i = 0; i < argc; i++) {
new_argv[i] = copy_and_allocate_user_string(args->argv[i]);
+ }
new_argv[argc] = NULL;
exec(filename, new_argv);
kfree((void *)filename);
- for (int i = 0; i < argc; i++)
+ for (int i = 0; i < argc; i++) {
kfree(new_argv[i]);
+ }
kfree(new_argv);
return -1;
}
@@ -52,8 +54,9 @@ int syscall_pread(SYS_PREAD_PARAMS *args) {
int syscall_read(SYS_READ_PARAMS *args) {
vfs_fd_t *fd = get_vfs_fd(args->fd);
- if (!fd)
+ if (!fd) {
return -EBADF;
+ }
int rc = vfs_pread(args->fd, args->buf, args->count, fd->offset);
fd->offset += rc;
return rc;
@@ -71,19 +74,22 @@ void syscall_exit(int status) {
void syscall_wait(int *status) {
disable_interrupts();
if (!get_current_task()->child) {
- if (status)
+ if (status) {
*status = -1;
+ }
return;
}
if (get_current_task()->child->dead) {
- if (status)
+ if (status) {
*status = get_current_task()->child_rc;
+ }
return;
}
get_current_task()->halts[WAIT_CHILD_HALT] = 1;
switch_task();
- if (status)
+ if (status) {
*status = get_current_task()->child_rc;
+ }
}
int syscall_fork(void) {
@@ -98,8 +104,9 @@ void *align_page(void *a);
int syscall_brk(void *addr) {
void *end = get_current_task()->data_segment_end;
- if (!mmu_allocate_region(end, addr - end, MMU_FLAG_RW, NULL))
+ if (!mmu_allocate_region(end, addr - end, MMU_FLAG_RW, NULL)) {
return -ENOMEM;
+ }
get_current_task()->data_segment_end = align_page(addr);
return 0;
}
@@ -110,8 +117,9 @@ void *syscall_sbrk(uintptr_t increment) {
void *n =
(void *)((uintptr_t)(get_current_task()->data_segment_end) + increment);
int rc2;
- if (0 > (rc2 = syscall_brk(n)))
+ if (0 > (rc2 = syscall_brk(n))) {
return (void *)rc2;
+ }
return rc;
}
diff --git a/kernel/crypto/ChaCha20/chacha20.c b/kernel/crypto/ChaCha20/chacha20.c
index 4486d00..9f5d6a5 100644
--- a/kernel/crypto/ChaCha20/chacha20.c
+++ b/kernel/crypto/ChaCha20/chacha20.c
@@ -10,8 +10,9 @@ void chacha_block(u32 out[16], u32 const in[16]) {
int i;
u32 x[16];
- for (i = 0; i < 16; ++i)
+ for (i = 0; i < 16; ++i) {
x[i] = in[i];
+ }
for (i = 0; i < ROUNDS; i += 2) {
QR(x[0], x[4], x[8], x[12]);
QR(x[1], x[5], x[9], x[13]);
@@ -23,6 +24,7 @@ void chacha_block(u32 out[16], u32 const in[16]) {
QR(x[2], x[7], x[8], x[13]);
QR(x[3], x[4], x[9], x[14]);
}
- for (i = 0; i < 16; ++i)
+ for (i = 0; i < 16; ++i) {
out[i] = x[i] + in[i];
+ }
}
diff --git a/kernel/drivers/ahci.c b/kernel/drivers/ahci.c
index 918551e..5a65408 100644
--- a/kernel/drivers/ahci.c
+++ b/kernel/drivers/ahci.c
@@ -181,10 +181,12 @@ u32 check_type(volatile struct HBA_PORT *port) {
u8 ipm = (ssts >> 8) & 0x0F;
u8 det = ssts & 0x0F;
- if (det != HBA_PORT_DET_PRESENT) // Check drive status
+ if (det != HBA_PORT_DET_PRESENT) { // Check drive status
return AHCI_DEV_NULL;
- if (ipm != HBA_PORT_IPM_ACTIVE)
+ }
+ if (ipm != HBA_PORT_IPM_ACTIVE) {
return AHCI_DEV_NULL;
+ }
switch (port->sig) {
case SATA_SIG_ATAPI:
@@ -380,8 +382,9 @@ u8 ahci_perform_command(volatile struct HBA_PORT *port, u32 startl, u32 starth,
for (;;) {
// In some longer duration reads, it may be helpful to spin on the DPS bit
// in the PxIS port field as well (1 << 5)
- if ((port->ci & (1 << command_slot)) == 0)
+ if ((port->ci & (1 << command_slot)) == 0) {
break;
+ }
if (port->is & HBA_PxIS_TFES) // Task file error
{
kprintf("Read disk error\n");
@@ -417,8 +420,9 @@ int ahci_write(u8 *buffer, u64 offset, u64 len, vfs_fd_t *fd) {
int rc = len;
u32 sector_count = len / 512;
- if (len % 512 != 0)
+ if (len % 512 != 0) {
sector_count++;
+ }
for (; sector_count >= num_prdt; lba++) {
ahci_raw_write(&hba->ports[port], lba, 0, num_prdt, (u16 *)buffer);
offset = 0;
@@ -445,8 +449,9 @@ int ahci_read(u8 *buffer, u64 offset, u64 len, vfs_fd_t *fd) {
int rc = len;
u32 sector_count = len / 512;
- if (len % 512 != 0)
+ if (len % 512 != 0) {
sector_count++;
+ }
u8 tmp_buffer[512 * num_prdt];
for (; sector_count >= num_prdt; lba++) {
ahci_raw_read(&hba->ports[port], lba, 0, num_prdt, (u16 *)tmp_buffer);
@@ -477,8 +482,9 @@ void add_devfs_drive_file(u8 port) {
void ahci_init(void) {
struct PCI_DEVICE device;
- if (!pci_devices_by_id(0x01, 0x06, &device))
+ if (!pci_devices_by_id(0x01, 0x06, &device)) {
return;
+ }
kprintf("vendor: %x\n", device.vendor);
kprintf("device: %x\n", device.device);
kprintf("header_type: %x\n", device.header_type);
@@ -489,8 +495,9 @@ void ahci_init(void) {
u8 *HBA_base = mmu_map_frames((void *)bar.address, bar.size);
hba = (volatile struct HBA_MEM *)(HBA_base);
for (u8 i = 0; i < 32; i++) {
- if (!((hba->pi >> i) & 1))
+ if (!((hba->pi >> i) & 1)) {
continue;
+ }
u32 type = check_type(&hba->ports[i]);
switch (type) {
case AHCI_DEV_SATA:
diff --git a/kernel/drivers/ata.c b/kernel/drivers/ata.c
index ee9e81b..0537f6e 100644
--- a/kernel/drivers/ata.c
+++ b/kernel/drivers/ata.c
@@ -43,8 +43,9 @@ int identify(int master_slave) {
outb(io_base + LBAmid, 0);
outb(io_base + LBAhi, 0);
outb(io_base + COMMAND_PORT, IDENTIFY);
- if (0 == inb(io_base + STATUS_PORT))
+ if (0 == inb(io_base + STATUS_PORT)) {
return 0; // Drive does not exist
+ }
for (; 0 != (inb(io_base + STATUS_PORT) & STATUS_BSY);)
;
@@ -67,8 +68,9 @@ int identify(int master_slave) {
return -2;
}
- if ((status & STATUS_DRQ))
+ if ((status & STATUS_DRQ)) {
break;
+ }
}
// The data is ready to read from the Data
@@ -86,8 +88,9 @@ int poll_status(void) {
// Read the Regular Status port until...
// We read this 15 times to give some
// time for the drive to catch up.
- for (int n = 0; n < 15; n++)
+ for (int n = 0; n < 15; n++) {
status = inb(io_base + STATUS_PORT);
+ }
// ERR or
// DF sets
@@ -98,8 +101,9 @@ int poll_status(void) {
// BSY clears
// DRQ sets
- if (0 == (status & STATUS_BSY) && 0 != (status & STATUS_DRQ))
+ if (0 == (status & STATUS_BSY) && 0 != (status & STATUS_DRQ)) {
break;
+ }
}
return 1;
}
@@ -160,8 +164,9 @@ void ata_write_lba28(u32 lba, u32 sector_count, volatile const u8 *buffer) {
// Wait for BSY to clear
for (;;) {
u16 status = inb(io_base + STATUS_PORT);
- if (!(status & STATUS_BSY))
+ if (!(status & STATUS_BSY)) {
break;
+ }
}
}
diff --git a/kernel/drivers/keyboard.c b/kernel/drivers/keyboard.c
index da04b1c..ec87390 100644
--- a/kernel/drivers/keyboard.c
+++ b/kernel/drivers/keyboard.c
@@ -90,12 +90,14 @@ u8 capital_ascii_table[] = {
vfs_inode_t *kb_inode;
u8 keyboard_to_ascii(u16 key, u8 capital) {
- if ((key & 0xFF) > sizeof(ascii_table))
+ if ((key & 0xFF) > sizeof(ascii_table)) {
return 'U';
- if (capital)
+ }
+ if (capital) {
return capital_ascii_table[key & 0xFF];
- else
+ } else {
return ascii_table[key & 0xFF];
+ }
}
u8 is_shift_down = 0;
diff --git a/kernel/drivers/mouse.c b/kernel/drivers/mouse.c
index 821bd1c..ec7ce8c 100644
--- a/kernel/drivers/mouse.c
+++ b/kernel/drivers/mouse.c
@@ -28,8 +28,9 @@ int fs_mouse_write(u8 *buffer, u64 offset, u64 len, vfs_fd_t *fd) {
int fs_mouse_read(u8 *buffer, u64 offset, u64 len, vfs_fd_t *fd) {
FIFO_FILE *f = fd->inode->internal_object;
- if (!mouse_inode->has_data)
+ if (!mouse_inode->has_data) {
return 0;
+ }
int rc = fifo_object_read(buffer, offset, len, f);
mouse_inode->has_data = f->has_data;
return rc;
diff --git a/kernel/drivers/pci.c b/kernel/drivers/pci.c
index 7484a41..464d400 100644
--- a/kernel/drivers/pci.c
+++ b/kernel/drivers/pci.c
@@ -13,8 +13,9 @@
// 0 Failure
u8 pci_get_bar(const struct PCI_DEVICE *device, u8 bar_index,
struct PCI_BaseAddressRegister *bar) {
- if (bar_index > 5)
+ if (bar_index > 5) {
return 0;
+ }
u8 offset = 0x10 + bar_index * sizeof(u32);
u32 physical_bar = pci_config_read32(device, 0, offset);
u32 original_bar = physical_bar;
@@ -90,10 +91,12 @@ u8 pci_devices_by_id(u8 class_id, u8 subclass_id,
u16 class_info = pci_config_read32(pci_device, 0, 0x8) >> 16;
u16 h_classcode = (class_info & 0xFF00) >> 8;
u16 h_subclass = (class_info & 0x00FF);
- if (h_classcode != class_id)
+ if (h_classcode != class_id) {
continue;
- if (h_subclass != subclass_id)
+ }
+ if (h_subclass != subclass_id) {
continue;
+ }
u32 device_vendor = pci_config_read32(pci_device, 0, 0);
pci_device->vendor = (device_vendor & 0xFFFF);
@@ -120,10 +123,12 @@ int pci_populate_device_struct(u16 vendor, u16 device,
tmp.bus = bus;
tmp.slot = slot;
u32 device_vendor = pci_config_read32(&tmp, 0, 0);
- if (vendor != (device_vendor & 0xFFFF))
+ if (vendor != (device_vendor & 0xFFFF)) {
continue;
- if (device != (device_vendor >> 16))
+ }
+ if (device != (device_vendor >> 16)) {
continue;
+ }
pci_device->bus = bus;
pci_device->slot = slot;
u32 bar0 = pci_config_read32(pci_device, 0, 0x10);
diff --git a/kernel/drivers/rtl8139.c b/kernel/drivers/rtl8139.c
index 34d9b85..b22bb8a 100644
--- a/kernel/drivers/rtl8139.c
+++ b/kernel/drivers/rtl8139.c
@@ -146,8 +146,9 @@ void get_mac_address(u8 mac[6]) {
u8 rtl8139_get_transmit_status(u32 base_address) {
u32 status_register = inl(base_address + 0x3E);
- if ((status_register >> 3) & 0x1)
+ if ((status_register >> 3) & 0x1) {
kprintf("transmit error :(\n");
+ }
u8 status = (status_register >> 2) & 0x1;
outl(base_address + 0x3E, 0x5);
return status;
@@ -200,6 +201,7 @@ void rtl8139_init(void) {
// ksbrk() seems to have the magical ability of disabling interrupts?
// I have no fucking clue why that happens and it was a pain to debug.
- for (int i = 0; i < 4; i++)
+ for (int i = 0; i < 4; i++) {
send_buffers[i] = ksbrk(0x1000);
+ }
}
diff --git a/kernel/elf.c b/kernel/elf.c
index 56ff893..3d03335 100644
--- a/kernel/elf.c
+++ b/kernel/elf.c
@@ -52,8 +52,9 @@ void *load_elf_file(const char *f, u32 *ds) {
flush_tlb();
uintptr_t e = program_header.p_vaddr + program_header.p_memsz;
- if (e > end_of_code)
+ if (e > end_of_code) {
end_of_code = e;
+ }
memset((void *)program_header.p_vaddr, 0, program_header.p_memsz);
diff --git a/kernel/fs/devfs.c b/kernel/fs/devfs.c
index c0ad7ce..2705dc8 100644
--- a/kernel/fs/devfs.c
+++ b/kernel/fs/devfs.c
@@ -29,9 +29,11 @@ vfs_inode_t *devfs_add_file(
}
vfs_inode_t *devfs_open(const char *file) {
- for (int i = 0; i < num_files; i++)
- if (isequal_n(files[i].name, file, strlen(files[i].name)))
+ for (int i = 0; i < num_files; i++) {
+ if (isequal_n(files[i].name, file, strlen(files[i].name))) {
return files[i].inode;
+ }
+ }
return 0;
}
@@ -53,8 +55,9 @@ int stdout_write(u8 *buffer, u64 offset, u64 len, vfs_fd_t *fd) {
(void)fd;
int rc = len;
- for (; len--;)
+ for (; len--;) {
putc(*buffer++);
+ }
return rc;
}
@@ -63,8 +66,9 @@ int serial_write(u8 *buffer, u64 offset, u64 len, vfs_fd_t *fd) {
(void)fd;
int rc = len;
- for (; len--;)
+ for (; len--;) {
write_serial(*buffer++);
+ }
return rc;
}
diff --git a/kernel/fs/ext2.c b/kernel/fs/ext2.c
index 60800a9..ccf2fdf 100644
--- a/kernel/fs/ext2.c
+++ b/kernel/fs/ext2.c
@@ -112,16 +112,18 @@ u32 num_block_groups(void) {
u32 num_blocks = superblock->num_blocks;
u32 num_blocks_in_group = superblock->num_blocks_in_group;
u32 b = num_blocks / num_blocks_in_group;
- if (num_blocks % num_blocks_in_group != 0)
+ if (num_blocks % num_blocks_in_group != 0) {
b++;
+ }
// Rounding up the total number of inodes divided by the number of
// inodes per block group
u32 num_inodes = superblock->num_inodes;
u32 num_inodes_in_group = superblock->num_inodes_in_group;
u32 i = num_inodes / num_inodes_in_group;
- if (num_inodes % num_inodes_in_group != 0)
+ if (num_inodes % num_inodes_in_group != 0) {
i++;
+ }
// Both (and check them against each other)
assert(i == b);
return i;
@@ -164,8 +166,9 @@ void ext2_get_inode_header(int inode_index, inode_t *data) {
}
void ext2_write_inode(int inode_index, inode_t *data) {
- if (ext2_last_inode_read == inode_index)
+ if (ext2_last_inode_read == inode_index) {
ext2_last_inode_read = -1; // Invalidate the cache
+ }
u32 block_index;
u32 block_offset;
ext2_block_containing_inode(inode_index, &block_index, &block_offset);
@@ -194,16 +197,20 @@ int ext2_get_inode_in_directory(int dir_inode, char *file,
for (; data_p <= (data_end - sizeof(direntry_header_t)) &&
(dir = (direntry_header_t *)data_p)->inode;
data_p += dir->size) {
- if (0 == dir->size)
+ if (0 == dir->size) {
break;
- if (0 == dir->name_length)
+ }
+ if (0 == dir->name_length) {
continue;
+ }
if (0 ==
memcmp(data_p + sizeof(direntry_header_t), file, dir->name_length)) {
- if (strlen(file) > dir->name_length)
+ if (strlen(file) > dir->name_length) {
continue;
- if (entry)
+ }
+ if (entry) {
memcpy(entry, data_p, sizeof(direntry_header_t));
+ }
return dir->inode;
}
}
@@ -225,12 +232,15 @@ int ext2_read_dir(int dir_inode, u8 *buffer, size_t len, size_t offset) {
for (; data_p <= (data_end - sizeof(direntry_header_t)) &&
(dir = (direntry_header_t *)data_p)->inode && len > 0;
data_p += dir->size, n_dir++) {
- if (0 == dir->size)
+ if (0 == dir->size) {
break;
- if (0 == dir->name_length)
+ }
+ if (0 == dir->name_length) {
continue;
- if (n_dir < (offset / sizeof(struct dirent)))
+ }
+ if (n_dir < (offset / sizeof(struct dirent))) {
continue;
+ }
memcpy(tmp_entry.d_name, data_p + sizeof(direntry_header_t),
dir->name_length);
@@ -250,8 +260,9 @@ int ext2_read_dir(int dir_inode, u8 *buffer, size_t len, size_t offset) {
u32 ext2_find_inode(const char *file) {
int cur_path_inode = EXT2_ROOT_INODE;
- if (*file == '/' && *(file + 1) == '\0')
+ if (*file == '/' && *(file + 1) == '\0') {
return cur_path_inode;
+ }
char *str = copy_and_allocate_string(file);
char *orig_str = str;
@@ -264,8 +275,9 @@ u32 ext2_find_inode(const char *file) {
for (; '/' != *str && '\0' != *str; str++)
;
- if ('\0' == *str)
+ if ('\0' == *str) {
final = 1;
+ }
*str = '\0';
@@ -276,8 +288,9 @@ u32 ext2_find_inode(const char *file) {
return 0;
}
- if (final)
+ if (final) {
break;
+ }
// The expected returned entry is a directory
if (TYPE_INDICATOR_DIRECTORY != a.type_indicator) {
@@ -298,8 +311,9 @@ u32 get_singly_block_index(u32 singly_block_ptr, u32 i) {
}
int get_block(inode_t *inode, u32 i) {
- if (i < 12)
+ if (i < 12) {
return inode->block_pointers[i];
+ }
i -= 12;
u32 singly_block_size = block_byte_size / (32 / 8);
@@ -356,8 +370,9 @@ int get_free_inode(int allocate) {
for (u32 g = 0; g < num_block_groups(); g++) {
get_group_descriptor(g, &block_group);
- if (0 == block_group.num_unallocated_inodes_in_group)
+ if (0 == block_group.num_unallocated_inodes_in_group) {
continue;
+ }
u8 bitmap[BLOCK_SIZE];
ext2_read_block(block_group.inode_usage_bitmap, bitmap, BLOCK_SIZE, 0);
@@ -388,22 +403,25 @@ int write_inode(int inode_num, u8 *data, u64 size, u64 offset, u64 *file_size,
inode_t *inode = (inode_t *)inode_buffer;
u64 fsize = (u64)(((u64)inode->_upper_32size << 32) | (u64)inode->low_32size);
- if (append)
+ if (append) {
offset = fsize;
+ }
u32 block_start = offset / block_byte_size;
u32 block_offset = offset % block_byte_size;
int num_blocks_used = inode->num_disk_sectors / (BLOCK_SIZE / SECTOR_SIZE);
- if (size + offset > fsize)
+ if (size + offset > fsize) {
fsize = size + offset;
+ }
int num_blocks_required = BLOCKS_REQUIRED(fsize, BLOCK_SIZE);
for (int i = num_blocks_used; i < num_blocks_required; i++) {
- if (i > 12)
+ if (i > 12) {
assert(0);
+ }
int b = get_free_block(1 /*true*/);
assert(-1 != b);
inode->block_pointers[i] = b;
@@ -442,17 +460,21 @@ int read_inode(int inode_num, u8 *data, u64 size, u64 offset, u64 *file_size) {
u64 fsize = (u64)(((u64)inode->_upper_32size << 32) | (u64)inode->low_32size);
- if (file_size)
+ if (file_size) {
*file_size = fsize;
+ }
- if (size > fsize - offset)
+ if (size > fsize - offset) {
size -= ((size + offset) - fsize);
+ }
- if (size == 0)
+ if (size == 0) {
return 0;
+ }
- if (offset > fsize)
+ if (offset > fsize) {
return 0;
+ }
u32 block_start = offset / block_byte_size;
u32 block_offset = offset % block_byte_size;
@@ -558,8 +580,9 @@ int ext2_truncate(vfs_fd_t *fd, size_t length) {
vfs_inode_t *ext2_open(const char *path) {
u32 inode_num = ext2_find_inode(path);
- if (0 == inode_num)
+ if (0 == inode_num) {
return NULL;
+ }
u8 buffer[inode_size];
inode_t *ext2_inode = (inode_t *)buffer;
@@ -601,10 +624,12 @@ u64 end_of_last_entry_position(int dir_inode, u64 *entry_offset,
for (; pos < file_size && (dir = (direntry_header_t *)data_p)->size;
data_p += dir->size, prev = pos, pos += dir->size)
;
- if (entry_offset)
+ if (entry_offset) {
*entry_offset = prev;
- if (meta)
+ }
+ if (meta) {
memcpy(meta, ((u8 *)data) + prev, sizeof(direntry_header_t));
+ }
kfree(data);
return pos;
}
@@ -633,8 +658,9 @@ void ext2_create_entry(int directory_inode, direntry_header_t entry_header,
entry_header.size += (4 - (entry_header.size % 4));
u32 length_till_next_block = 1024 - (new_entry_offset % 1024);
- if (0 == length_till_next_block)
+ if (0 == length_till_next_block) {
length_till_next_block = 1024;
+ }
assert(entry_header.size < length_till_next_block);
entry_header.size = length_till_next_block;
@@ -659,8 +685,9 @@ int ext2_find_parent(char *path, u32 *parent_inode, char **filename) {
return 1;
} else {
int r = ext2_find_inode(path);
- if (0 == r)
+ if (0 == r) {
return 0;
+ }
*parent_inode = r;
return 1;
}
@@ -799,8 +826,9 @@ void parse_superblock(void) {
; // TODO: Fail properly
}
- if (1 <= superblock->major_version)
+ if (1 <= superblock->major_version) {
inode_size = ((ext_superblock_t *)superblock)->inode_size;
+ }
inodes_per_block = block_byte_size / inode_size;
}
diff --git a/kernel/fs/fifo.c b/kernel/fs/fifo.c
index 4ae7c8a..9844b9e 100644
--- a/kernel/fs/fifo.c
+++ b/kernel/fs/fifo.c
@@ -28,12 +28,14 @@ int fifo_object_read(u8 *buffer, u64 offset, u64 len, FIFO_FILE *file) {
return -EAGAIN;
}
- if (len == 0)
+ if (len == 0) {
return 0;
+ }
file->can_write = 1;
- if (len > file->write_len)
+ if (len > file->write_len) {
len = file->write_len;
+ }
memcpy(buffer, file->buffer, len);
// Shift bufffer to the left
diff --git a/kernel/fs/shm.c b/kernel/fs/shm.c
index 5eb7f23..e0c0211 100644
--- a/kernel/fs/shm.c
+++ b/kernel/fs/shm.c
@@ -16,11 +16,13 @@ void shm_init(void) {
int shm_write(u8 *buffer, u64 offset, u64 len, vfs_fd_t *fd) {
vfs_vm_object_t *p = fd->inode->internal_object;
- if (offset > p->size)
+ if (offset > p->size) {
return -EFBIG;
+ }
- if (offset + len > p->size)
+ if (offset + len > p->size) {
len = p->size - offset;
+ }
memcpy((void *)((uintptr_t)((uintptr_t)p->virtual_object + offset)), buffer,
len);
@@ -30,11 +32,13 @@ int shm_write(u8 *buffer, u64 offset, u64 len, vfs_fd_t *fd) {
int shm_read(u8 *buffer, u64 offset, u64 len, vfs_fd_t *fd) {
vfs_vm_object_t *p = fd->inode->internal_object;
- if (offset > p->size)
+ if (offset > p->size) {
return -EFBIG;
+ }
- if (offset + len > p->size)
+ if (offset + len > p->size) {
len = p->size - offset;
+ }
memcpy((void *)buffer,
(void *)((uintptr_t)((uintptr_t)p->virtual_object + offset)), len);
@@ -54,10 +58,11 @@ int shm_ftruncate(vfs_fd_t *fd, size_t length) {
p->virtual_object = ksbrk(length);
int n = (uintptr_t)align_page((void *)(u32)length) / 0x1000;
p->object = kmalloc(sizeof(void *) * n);
- for (int i = 0; i < n; i++)
+ for (int i = 0; i < n; i++) {
p->object[i] =
(void *)(get_page(p->virtual_object + (i * 0x1000), NULL, 0, 0)->frame *
0x1000);
+ }
return 0;
}
diff --git a/kernel/fs/tmpfs.c b/kernel/fs/tmpfs.c
index b9f7b7e..1054a27 100644
--- a/kernel/fs/tmpfs.c
+++ b/kernel/fs/tmpfs.c
@@ -14,8 +14,9 @@ void tmp_close(vfs_fd_t *fd) {
int tmp_write(u8 *buffer, u64 offset, u64 len, vfs_fd_t *fd) {
tmp_inode *calling_file = fd->inode->internal_object;
tmp_inode *child_file = calling_file->read_inode->internal_object;
- if (child_file->is_closed)
+ if (child_file->is_closed) {
return -EPIPE;
+ }
int rc = fifo_object_write(buffer, offset, len, child_file->fifo);
calling_file->read_inode->has_data = child_file->fifo->has_data;
@@ -26,8 +27,9 @@ int tmp_write(u8 *buffer, u64 offset, u64 len, vfs_fd_t *fd) {
int tmp_read(u8 *buffer, u64 offset, u64 len, vfs_fd_t *fd) {
tmp_inode *calling_file = fd->inode->internal_object;
tmp_inode *child_file = calling_file->read_inode->internal_object;
- if (calling_file->is_closed)
+ if (calling_file->is_closed) {
return -EPIPE;
+ }
int rc = fifo_object_read(buffer, offset, len, calling_file->fifo);
fd->inode->has_data = calling_file->fifo->has_data;
diff --git a/kernel/fs/vfs.c b/kernel/fs/vfs.c
index 214cfd3..2a072e9 100644
--- a/kernel/fs/vfs.c
+++ b/kernel/fs/vfs.c
@@ -58,11 +58,14 @@ int vfs_create_fd(int flags, int mode, int is_tty, vfs_inode_t *inode,
vfs_fd_t **fd) {
process_t *p = (process_t *)get_current_task();
int i;
- for (i = 0; i < 100; i++)
- if (!p->file_descriptors[i])
+ for (i = 0; i < 100; i++) {
+ if (!p->file_descriptors[i]) {
break;
- if (p->file_descriptors[i])
+ }
+ }
+ if (p->file_descriptors[i]) {
return -1;
+ }
vfs_fd_t *r = kmalloc(sizeof(vfs_fd_t));
r->flags = flags;
r->mode = mode;
@@ -71,8 +74,9 @@ int vfs_create_fd(int flags, int mode, int is_tty, vfs_inode_t *inode,
r->is_tty = is_tty;
r->offset = 0;
p->file_descriptors[i] = r;
- if (fd)
+ if (fd) {
*fd = r;
+ }
return i;
}
@@ -81,16 +85,18 @@ int vfs_create_file(const char *file) {
int length = 0;
for (int i = 0; i < num_mounts; i++) {
int path_len = strlen(mounts[i].path);
- if (path_len <= length)
+ if (path_len <= length) {
continue;
+ }
if (isequal_n(mounts[i].path, file, path_len)) {
length = path_len;
file_mount = &mounts[i];
}
}
- if (1 != length)
+ if (1 != length) {
file += length;
+ }
if (!file_mount) {
kprintf("vfs_internal_open could not find mounted path for file : %s\n",
@@ -106,16 +112,18 @@ vfs_inode_t *vfs_internal_open(const char *file) {
int length = 0;
for (int i = 0; i < num_mounts; i++) {
int path_len = strlen(mounts[i].path);
- if (path_len <= length)
+ if (path_len <= length) {
continue;
+ }
if (isequal_n(mounts[i].path, file, path_len)) {
length = path_len;
file_mount = &mounts[i];
}
}
- if (1 != length)
+ if (1 != length) {
file += length;
+ }
if (!file_mount) {
kprintf("vfs_internal_open could not find mounted path for file : %s\n",
@@ -130,8 +138,9 @@ vfs_inode_t *vfs_internal_open(const char *file) {
// Does canonicalization of absolute paths
int vfs_clean_path(const char *path, char *result) {
// It has to be a absolute path
- if ('/' != *path)
+ if ('/' != *path) {
return 0;
+ }
const char *result_start = result;
int start_directory = 0;
int should_insert_slash = 0;
@@ -149,8 +158,9 @@ int vfs_clean_path(const char *path, char *result) {
// "/../foo". A "/.." should become a "/"
// Therefore it skips going back to the parent
if (*path == '/') {
- if (result_start == result)
+ if (result_start == result) {
return 0;
+ }
result--;
}
} else {
@@ -165,8 +175,9 @@ int vfs_clean_path(const char *path, char *result) {
}
}
start_directory = ('/' == *path);
- if ('\0' == *path)
+ if ('\0' == *path) {
break;
+ }
*result = *path;
result++;
}
@@ -196,8 +207,9 @@ char *vfs_resolve_path(const char *file, char *resolved_path) {
int vfs_fstat(int fd, struct stat *buf) {
vfs_fd_t *fd_ptr = get_vfs_fd(fd);
- if (!fd_ptr)
+ if (!fd_ptr) {
return -EBADF;
+ }
assert(fd_ptr->inode->stat);
return fd_ptr->inode->stat(fd_ptr, buf);
}
@@ -207,8 +219,9 @@ int vfs_chdir(const char *path) {
char *resolved_path = vfs_resolve_path(path, tmp_path);
{
int tmp_fd = vfs_open(resolved_path, O_READ, 0);
- if (0 > tmp_fd)
+ if (0 > tmp_fd) {
return tmp_fd;
+ }
struct stat stat_result;
vfs_fstat(tmp_fd, &stat_result);
if (STAT_DIR != stat_result.st_mode) {
@@ -218,8 +231,10 @@ int vfs_chdir(const char *path) {
vfs_close(tmp_fd);
}
strcpy(get_current_task()->current_working_directory, resolved_path);
- if ('/' != resolved_path[strlen(resolved_path)] && strlen(resolved_path) != 1)
+ if ('/' != resolved_path[strlen(resolved_path)] &&
+ strlen(resolved_path) != 1) {
strcat(get_current_task()->current_working_directory, "/");
+ }
return 0;
}
@@ -228,16 +243,18 @@ int vfs_mkdir(const char *path, int mode) {
int length = 0;
for (int i = 0; i < num_mounts; i++) {
int path_len = strlen(mounts[i].path);
- if (path_len <= length)
+ if (path_len <= length) {
continue;
+ }
if (isequal_n(mounts[i].path, path, path_len)) {
length = path_len;
file_mount = &mounts[i];
}
}
- if (1 != length)
+ if (1 != length) {
path += length;
+ }
if (!file_mount) {
kprintf("vfs_internal_open could not find mounted path for file : %s\n",
@@ -283,8 +300,9 @@ int vfs_close(int fd) {
get_current_task()->file_descriptors[fd] = 0;
// If no references left then free the contents
if (0 == fd_ptr->reference_count) {
- if (fd_ptr->inode->close)
+ if (fd_ptr->inode->close) {
fd_ptr->inode->close(fd_ptr);
+ }
kfree(fd_ptr);
}
@@ -292,8 +310,9 @@ int vfs_close(int fd) {
}
int raw_vfs_pread(vfs_fd_t *vfs_fd, void *buf, u64 count, u64 offset) {
- if (!(vfs_fd->flags & O_READ))
+ if (!(vfs_fd->flags & O_READ)) {
return -EBADF;
+ }
return vfs_fd->inode->read(buf, offset, count, vfs_fd);
}
@@ -308,8 +327,9 @@ int vfs_pread(int fd, void *buf, u64 count, u64 offset) {
return -EBADF;
}
vfs_fd_t *vfs_fd = get_current_task()->file_descriptors[fd];
- if (!vfs_fd)
+ if (!vfs_fd) {
return -EBADF;
+ }
int rc = raw_vfs_pread(vfs_fd, buf, count, offset);
if (-EAGAIN == rc && count > 0) {
if (!(vfs_fd->flags & O_NONBLOCK)) {
@@ -333,8 +353,9 @@ int raw_vfs_pwrite(vfs_fd_t *vfs_fd, void *buf, u64 count, u64 offset) {
int vfs_pwrite(int fd, void *buf, u64 count, u64 offset) {
vfs_fd_t *vfs_fd = get_vfs_fd(fd);
- if (!vfs_fd)
+ if (!vfs_fd) {
return -EBADF;
+ }
if (!(vfs_fd->flags & O_WRITE)) {
return -EBADF;
}
@@ -343,8 +364,9 @@ int vfs_pwrite(int fd, void *buf, u64 count, u64 offset) {
vfs_vm_object_t *vfs_get_vm_object(int fd, u64 length, u64 offset) {
vfs_fd_t *vfs_fd = get_vfs_fd(fd);
- if (!vfs_fd)
+ if (!vfs_fd) {
return NULL;
+ }
vfs_vm_object_t *r = vfs_fd->inode->get_vm_object(length, offset, vfs_fd);
return r;
}
@@ -358,15 +380,19 @@ int vfs_dup2(int org_fd, int new_fd) {
int vfs_ftruncate(int fd, size_t length) {
vfs_fd_t *fd_ptr = get_vfs_fd(fd);
- if (!fd_ptr)
+ if (!fd_ptr) {
return -EBADF;
- if (!(fd_ptr->flags & O_READ))
+ }
+ if (!(fd_ptr->flags & O_READ)) {
return -EINVAL;
+ }
vfs_inode_t *inode = fd_ptr->inode;
- if (!inode)
+ if (!inode) {
return -EINVAL;
- if (!inode->truncate)
+ }
+ if (!inode->truncate) {
return -EINVAL;
+ }
return inode->truncate(fd_ptr, length);
}
diff --git a/kernel/halts.c b/kernel/halts.c
index c2b682e..c5afdac 100644
--- a/kernel/halts.c
+++ b/kernel/halts.c
@@ -5,6 +5,9 @@
int isset_fdhalt(process_t *p) {
int blocked = 0;
struct list *read_list = &p->read_list;
+ struct list *write_list = &p->write_list;
+ struct list *disconnect_list = &p->disconnect_list;
+
for (int i = 0;; i++) {
vfs_inode_t *inode;
if (!list_get(read_list, i, (void **)&inode)) {
@@ -15,7 +18,6 @@ int isset_fdhalt(process_t *p) {
}
blocked = 1;
}
- struct list *write_list = &p->write_list;
for (int i = 0;; i++) {
vfs_inode_t *inode;
if (!list_get(write_list, i, (void **)&inode)) {
@@ -26,7 +28,6 @@ int isset_fdhalt(process_t *p) {
}
blocked = 1;
}
- struct list *disconnect_list = &p->disconnect_list;
for (int i = 0;; i++) {
vfs_inode_t *inode;
if (!list_get(disconnect_list, i, (void **)&inode)) {
diff --git a/kernel/hashmap/hashmap.c b/kernel/hashmap/hashmap.c
index 8779f0b..833ed70 100644
--- a/kernel/hashmap/hashmap.c
+++ b/kernel/hashmap/hashmap.c
@@ -60,8 +60,9 @@ char *copy_c_string(const char *str) {
char *ret_string;
size_t len = strlen(str);
ret_string = kmalloc(len + 1);
- if (!ret_string)
+ if (!ret_string) {
return NULL;
+ }
memcpy(ret_string, str, len);
ret_string[len] = '\0';
return ret_string;
@@ -83,26 +84,32 @@ void free_linkedlist_entry(LinkedList *entry) {
LinkedList *get_linkedlist_entry(LinkedList *list, const char *key,
LinkedList **prev) {
- if (prev)
+ if (prev) {
*prev = NULL;
+ }
for (; list; list = list->next) {
const char *str1 = key;
const char *str2 = list->key;
- for (; *str1 && *str2; str1++, str2++)
- if (*str1 != *str2)
+ for (; *str1 && *str2; str1++, str2++) {
+ if (*str1 != *str2) {
break;
- if (*str1 == *str2)
+ }
+ }
+ if (*str1 == *str2) {
return list;
- if (prev)
+ }
+ if (prev) {
prev = &list;
+ }
}
return NULL;
}
void *get_linkedlist_value(LinkedList *list, const char *key) {
LinkedList *entry = get_linkedlist_entry(list, key, NULL);
- if (!entry)
+ if (!entry) {
return NULL;
+ }
return entry->value;
}
@@ -115,15 +122,17 @@ int hashmap_add_entry(HashMap *m, const char *key, void *value,
int do_not_allocate_key) {
// Create the entry
LinkedList *entry = kmalloc(sizeof(LinkedList));
- if (!entry)
+ if (!entry) {
return 0;
+ }
entry->key_allocated = !do_not_allocate_key;
if (do_not_allocate_key) {
entry->key = key;
} else {
- if (!(entry->key = copy_c_string(key)))
+ if (!(entry->key = copy_c_string(key))) {
return 0;
+ }
}
entry->value = value;
entry->next = NULL;
@@ -132,8 +141,9 @@ int hashmap_add_entry(HashMap *m, const char *key, void *value,
// Add the new entry to the list.
u32 index = find_index(m, key);
LinkedList **list_pointer = &m->entries[index];
- for (; *list_pointer;)
+ for (; *list_pointer;) {
list_pointer = &(*list_pointer)->next;
+ }
*list_pointer = entry;
m->num_entries++;
@@ -142,31 +152,37 @@ int hashmap_add_entry(HashMap *m, const char *key, void *value,
void *hashmap_get_entry(HashMap *m, const char *key) {
u32 index = find_index(m, key);
- if (!m->entries[index])
+ if (!m->entries[index]) {
return NULL;
+ }
return get_linkedlist_value(m->entries[index], key);
}
int hashmap_delete_entry(HashMap *m, const char *key) {
LinkedList *list = m->entries[find_index(m, key)];
- if (!list)
+ if (!list) {
return 0;
+ }
LinkedList **prev = NULL;
LinkedList *entry = get_linkedlist_entry(list, key, prev);
- if (!entry)
+ if (!entry) {
return 0;
- if (!prev)
+ }
+ if (!prev) {
prev = &m->entries[find_index(m, key)];
+ }
- if (entry->upon_deletion)
+ if (entry->upon_deletion) {
entry->upon_deletion(entry->key, entry->value);
+ }
LinkedList *next = entry->next;
free_linkedlist_entry(entry);
- if (*prev != entry)
+ if (*prev != entry) {
(*prev)->next = next;
- else
+ } else {
*prev = NULL;
+ }
// Redo the delete process incase there are multiple
// entires that have the same key.
@@ -177,12 +193,14 @@ int hashmap_delete_entry(HashMap *m, const char *key) {
void hashmap_free(HashMap *m) {
for (int i = 0; i < m->size; i++) {
- if (!m->entries[i])
+ if (!m->entries[i]) {
continue;
+ }
LinkedList *list = m->entries[i];
for (; list;) {
- if (list->upon_deletion)
+ if (list->upon_deletion) {
list->upon_deletion(list->key, list->value);
+ }
LinkedList *old = list;
list = list->next;
free_linkedlist_entry(old);
@@ -194,8 +212,9 @@ void hashmap_free(HashMap *m) {
HashMap *hashmap_create(size_t size) {
HashMap *m = kmalloc(sizeof(HashMap));
- if (!m)
+ if (!m) {
return NULL;
+ }
m->size = size;
m->num_entries = 0;
@@ -203,11 +222,13 @@ HashMap *hashmap_create(size_t size) {
// Create a array of pointers to linkedlists but don't create them
// yet.
m->entries = kcalloc(size, sizeof(LinkedList **));
- if (!m->entries)
+ if (!m->entries) {
return NULL;
+ }
- for (size_t i = 0; i < m->size; i++)
+ for (size_t i = 0; i < m->size; i++) {
m->entries[i] = NULL;
+ }
m->hash_function = hash;
return m;
diff --git a/kernel/kmalloc.c b/kernel/kmalloc.c
index 6f8f520..f362f6b 100644
--- a/kernel/kmalloc.c
+++ b/kernel/kmalloc.c
@@ -74,45 +74,53 @@ MallocHeader *next_close_header(MallocHeader *a) {
for (;;)
;
}
- if (a->flags & IS_FINAL)
+ if (a->flags & IS_FINAL) {
return NULL;
+ }
return next_header(a);
}
MallocHeader *find_free_entry(u32 s) {
// A new header is required as well as the newly allocated chunk
s += sizeof(MallocHeader);
- if (!head)
+ if (!head) {
init_heap();
+ }
MallocHeader *p = head;
for (; p; p = next_header(p)) {
assert(p->magic == 0xdde51ab9410268b1);
- if (!(p->flags & IS_FREE))
+ if (!(p->flags & IS_FREE)) {
continue;
+ }
u64 required_size = s;
- if (p->size < required_size)
+ if (p->size < required_size) {
continue;
+ }
return p;
}
return NULL;
}
void merge_headers(MallocHeader *b) {
- if (!(b->flags & IS_FREE))
+ if (!(b->flags & IS_FREE)) {
return;
+ }
MallocHeader *n = next_close_header(b);
- if (!n)
+ if (!n) {
return;
+ }
- if (!(n->flags & IS_FREE))
+ if (!(n->flags & IS_FREE)) {
return;
+ }
b->size += n->size;
b->flags |= n->flags & IS_FINAL;
b->n = n->n;
- if (n == final)
+ if (n == final) {
final = b;
+ }
}
void *kmalloc(size_t s) {
@@ -136,8 +144,9 @@ void *kmalloc(size_t s) {
new_entry->size = free_entry->size - n - sizeof(MallocHeader);
new_entry->magic = 0xdde51ab9410268b1;
- if (free_entry == final)
+ if (free_entry == final) {
final = new_entry;
+ }
merge_headers(new_entry);
// Modify the free entry
@@ -150,17 +159,20 @@ void *kmalloc(size_t s) {
}
size_t get_mem_size(void *ptr) {
- if (!ptr)
+ if (!ptr) {
return 0;
+ }
return ((MallocHeader *)((uintptr_t)ptr - sizeof(MallocHeader)))->size;
}
void *krealloc(void *ptr, size_t size) {
void *rc = kmalloc(size);
- if (!rc)
+ if (!rc) {
return NULL;
- if (!ptr)
+ }
+ if (!ptr) {
return rc;
+ }
size_t l = get_mem_size(ptr);
size_t to_copy = min(l, size);
memcpy(rc, ptr, to_copy);
@@ -187,21 +199,24 @@ void *kallocarray(size_t nmemb, size_t size) {
void *kcalloc(size_t nelem, size_t elsize) {
void *rc = kallocarray(nelem, elsize);
- if (!rc)
+ if (!rc) {
return NULL;
+ }
memset(rc, 0, nelem * elsize);
return rc;
}
void kfree(void *p) {
- if (!p)
+ if (!p) {
return;
+ }
// FIXME: This assumes that p is at the start of a allocated area.
// Could this be avoided in a simple way?
MallocHeader *h = (MallocHeader *)((uintptr_t)p - sizeof(MallocHeader));
assert(h->magic == 0xdde51ab9410268b1);
- if (h->flags & IS_FREE)
+ if (h->flags & IS_FREE) {
return;
+ }
h->flags |= IS_FREE;
merge_headers(h);
diff --git a/kernel/kubsan.c b/kernel/kubsan.c
index 87b2932..fc2900d 100644
--- a/kernel/kubsan.c
+++ b/kernel/kubsan.c
@@ -1,7 +1,7 @@
+#include <interrupts.h>
#include <kubsan.h>
#include <log.h>
#include <stdio.h>
-#include <interrupts.h>
void ubsan_log(const char *cause, struct source_location source) {
kprintf("%s: %s : %d\n", cause, source.file_name, source.line);
diff --git a/kernel/lib/buffered_write.c b/kernel/lib/buffered_write.c
index e948e00..b6b78f9 100644
--- a/kernel/lib/buffered_write.c
+++ b/kernel/lib/buffered_write.c
@@ -1,5 +1,5 @@
-#include <lib/buffered_write.h>
#include <kmalloc.h>
+#include <lib/buffered_write.h>
int buffered_init(struct buffered *ctx, u16 size) {
ctx->data = kmalloc(size);
diff --git a/kernel/lib/stack.h b/kernel/lib/stack.h
index e514a04..efb9cb1 100644
--- a/kernel/lib/stack.h
+++ b/kernel/lib/stack.h
@@ -1,7 +1,7 @@
#include <stdint.h>
-//struct entry;
-//struct stack;
+// struct entry;
+// struct stack;
struct entry {
void *ptr;
diff --git a/kernel/libc/stdio/print.c b/kernel/libc/stdio/print.c
index fe50f50..93393ee 100644
--- a/kernel/libc/stdio/print.c
+++ b/kernel/libc/stdio/print.c
@@ -23,12 +23,14 @@ int kprint_hex(u64 num) {
char str[16] = {0};
int i = 0;
- for (; num != 0 && i < 16; i++, num /= 16)
+ for (; num != 0 && i < 16; i++, num /= 16) {
str[i] = HEX_SET[(num % 16)];
+ }
c += i;
- for (i--; i >= 0; i--)
+ for (i--; i >= 0; i--) {
putc(str[i]);
+ }
return c;
}
@@ -42,12 +44,14 @@ int kprint_int(int num) {
}
char str[10];
int i = 0;
- for (; num != 0 && i < 10; i++, num /= 10)
+ for (; num != 0 && i < 10; i++, num /= 10) {
str[i] = (num % 10) + '0';
+ }
c += i;
- for (i--; i >= 0; i--)
+ for (i--; i >= 0; i--) {
putc(str[i]);
+ }
return c;
}
@@ -65,8 +69,9 @@ int kprintf(const char *format, ...) {
}
char flag = *(s + 1);
- if ('\0' == flag)
+ if ('\0' == flag) {
break;
+ }
switch (flag) {
case 'c':
diff --git a/kernel/libc/string/copy.c b/kernel/libc/string/copy.c
index 277c808..166924b 100644
--- a/kernel/libc/string/copy.c
+++ b/kernel/libc/string/copy.c
@@ -6,21 +6,24 @@
char *copy_and_allocate_string(const char *s) {
size_t l = strlen(s);
char *r = kmalloc(l + 1);
- if (!r)
+ if (!r) {
return NULL;
+ }
return strncpy(r, s, l);
}
char *copy_and_allocate_user_string(const char *s) {
size_t len;
- if (!is_valid_user_c_string(s, &len))
+ if (!is_valid_user_c_string(s, &len)) {
return NULL;
+ }
size_t real_len = strlen(s);
assert(real_len == len);
len = real_len;
char *r = kmalloc(len + 1);
- if (!r)
+ if (!r) {
return NULL;
+ }
strlcpy(r, s, len);
return r;
}
diff --git a/kernel/libc/string/isequal.c b/kernel/libc/string/isequal.c
index 90712db..c3dbebb 100644
--- a/kernel/libc/string/isequal.c
+++ b/kernel/libc/string/isequal.c
@@ -1,15 +1,19 @@
#include "../include/string.h"
int isequal(const char *s1, const char *s2) {
- for (; *s1; s1++, s2++)
- if (*s1 != *s2)
+ for (; *s1; s1++, s2++) {
+ if (*s1 != *s2) {
return 0;
+ }
+ }
return 1;
}
int isequal_n(const char *s1, const char *s2, u32 n) {
- for (; *s1 && n; s1++, s2++, n--)
- if (*s1 != *s2)
+ for (; *s1 && n; s1++, s2++, n--) {
+ if (*s1 != *s2) {
return 0;
+ }
+ }
return 1;
}
diff --git a/kernel/libc/string/memcmp.c b/kernel/libc/string/memcmp.c
index deeb029..5631d05 100644
--- a/kernel/libc/string/memcmp.c
+++ b/kernel/libc/string/memcmp.c
@@ -3,9 +3,11 @@
int memcmp(const void *s1, const void *s2, u32 n) {
int return_value = 0;
- for (u32 i = 0; i < n; i++)
- if (((unsigned char *)(s1))[i] != ((unsigned char *)(s2))[i])
+ for (u32 i = 0; i < n; i++) {
+ if (((unsigned char *)(s1))[i] != ((unsigned char *)(s2))[i]) {
return_value++;
+ }
+ }
return return_value;
}
diff --git a/kernel/libc/string/memcpy.c b/kernel/libc/string/memcpy.c
index 8b03536..ac7a57b 100644
--- a/kernel/libc/string/memcpy.c
+++ b/kernel/libc/string/memcpy.c
@@ -4,16 +4,20 @@ void *memcpy(void *dest, const void *src, u32 n) {
unsigned char *d = dest;
const unsigned char *s = src;
- for (; n >= 8; n -= 8, d += 8, s += 8)
+ for (; n >= 8; n -= 8, d += 8, s += 8) {
*(u64 *)d = *(u64 *)s;
+ }
- for (; n >= 4; n -= 4, d += 4, s += 4)
+ for (; n >= 4; n -= 4, d += 4, s += 4) {
*(u32 *)d = *(u32 *)s;
+ }
- for (; n >= 2; n -= 2, d += 2, s += 2)
+ for (; n >= 2; n -= 2, d += 2, s += 2) {
*(u16 *)d = *(u16 *)s;
+ }
- for (; n; n--)
+ for (; n; n--) {
*d++ = *s++;
+ }
return dest;
}
diff --git a/kernel/libc/string/memset.c b/kernel/libc/string/memset.c
index 5d7de62..34fd507 100644
--- a/kernel/libc/string/memset.c
+++ b/kernel/libc/string/memset.c
@@ -2,8 +2,9 @@
void *memset(void *dst, const unsigned char c, u32 n) {
uintptr_t d = (uintptr_t)dst;
- for (u32 i = 0; i < n; i++, d++)
+ for (u32 i = 0; i < n; i++, d++) {
*(unsigned char *)d = c;
+ }
return (void *)d;
}
diff --git a/kernel/libc/string/strlcpy.c b/kernel/libc/string/strlcpy.c
index 43d0e58..1338699 100644
--- a/kernel/libc/string/strlcpy.c
+++ b/kernel/libc/string/strlcpy.c
@@ -8,12 +8,14 @@ size_t strlcpy(char *dst, const char *src, size_t dsize) {
size_t n = dsize;
const char *osrc = src;
for (; n; n--) {
- if ((*dst++ = *src++) == '\0')
+ if ((*dst++ = *src++) == '\0') {
break;
+ }
}
if (n == 0) {
- if (dsize != 0)
+ if (dsize != 0) {
*dst = '\0'; /* NUL-terminate dst */
+ }
while (*src++)
;
}
diff --git a/kernel/network/arp.c b/kernel/network/arp.c
index 6e589ec..bd3cd24 100644
--- a/kernel/network/arp.c
+++ b/kernel/network/arp.c
@@ -46,8 +46,9 @@ void print_mac(const char *str, u8 *mac) {
kprintf("%s: ", str);
for (int i = 0; i < 6; i++) {
kprintf("%x", mac[i]);
- if (5 != i)
+ if (5 != i) {
kprintf(":");
+ }
}
kprintf("\n");
}
@@ -56,8 +57,9 @@ void print_ip(const char *str, const u8 *ip) {
kprintf("%s: ", str);
for (int i = 0; i < 4; i++) {
kprintf("%d", ip[i]);
- if (3 != i)
+ if (3 != i) {
kprintf(".");
+ }
}
kprintf("\n");
}
@@ -84,8 +86,9 @@ void send_arp_request(const u8 ip[4]) {
int get_mac_from_ip(const u8 ip[4], u8 mac[6]) {
for (int i = 0; i < 10; i++) {
- if (0 != memcmp(arp_table[i].ip, ip, sizeof(u8[4])))
+ if (0 != memcmp(arp_table[i].ip, ip, sizeof(u8[4]))) {
continue;
+ }
memcpy(mac, arp_table[i].mac, sizeof(u8[6]));
return 1;
}
@@ -93,8 +96,9 @@ int get_mac_from_ip(const u8 ip[4], u8 mac[6]) {
send_arp_request(ip);
// TODO: Maybe wait a bit?
for (int i = 0; i < 10; i++) {
- if (0 != memcmp(arp_table[i].ip, ip, sizeof(u8[4])))
+ if (0 != memcmp(arp_table[i].ip, ip, sizeof(u8[4]))) {
continue;
+ }
memcpy(mac, arp_table[i].mac, sizeof(u8[6]));
return 1;
}
diff --git a/kernel/network/ethernet.c b/kernel/network/ethernet.c
index 940802f..4fe6a94 100644
--- a/kernel/network/ethernet.c
+++ b/kernel/network/ethernet.c
@@ -28,8 +28,9 @@ u32 crc32(const char *buf, size_t len) {
if (rem & 1) {
rem >>= 1;
rem ^= 0xedb88320;
- } else
+ } else {
rem >>= 1;
+ }
}
table[i] = rem;
}
diff --git a/kernel/network/ipv4.c b/kernel/network/ipv4.c
index 6aa03ba..6335a09 100644
--- a/kernel/network/ipv4.c
+++ b/kernel/network/ipv4.c
@@ -73,8 +73,9 @@ void handle_ipv4(const u8 *payload, u32 packet_length) {
*(u16 *)(payload + 10) = 0;
u16 calc_checksum = ip_checksum((u8 *)payload, 20);
*(u16 *)(payload + 10) = saved_checksum;
- if (calc_checksum != saved_checksum)
+ if (calc_checksum != saved_checksum) {
return;
+ }
u8 version = (*payload & 0xF0) >> 4;
u8 IHL = (*payload & 0xF);
diff --git a/kernel/network/tcp.c b/kernel/network/tcp.c
index a3ba7cd..f45d1af 100644
--- a/kernel/network/tcp.c
+++ b/kernel/network/tcp.c
@@ -55,8 +55,9 @@ u16 tcp_checksum(u16 *buffer, int size) {
cksum += *buffer++;
size -= sizeof(u16);
}
- if (size)
+ if (size) {
cksum += *(u8 *)buffer;
+ }
cksum = (cksum >> 16) + (cksum & 0xffff);
cksum += (cksum >> 16);
@@ -179,8 +180,9 @@ void handle_tcp(u8 src_ip[4], const u8 *payload, u32 payload_length) {
if (SYN == flags) {
struct INCOMING_TCP_CONNECTION *inc;
- if (!(inc = handle_incoming_tcp_connection(src_ip, n_src_port, dst_port)))
+ if (!(inc = handle_incoming_tcp_connection(src_ip, n_src_port, dst_port))) {
return;
+ }
memcpy(inc->ip, src_ip, sizeof(u8[4]));
inc->seq_num = 0;
inc->ack_num = seq_num + 1;
@@ -192,8 +194,9 @@ void handle_tcp(u8 src_ip[4], const u8 *payload, u32 payload_length) {
}
struct INCOMING_TCP_CONNECTION *inc =
get_incoming_tcp_connection(src_ip, n_src_port);
- if (!inc)
+ if (!inc) {
return;
+ }
if (flags == (FIN | ACK)) {
if (inc->requesting_connection_close) {
send_empty_tcp_message(inc, ACK, seq_num, n_dst_port, n_src_port);
diff --git a/kernel/poll.c b/kernel/poll.c
index 0452bc8..963bfcc 100644
--- a/kernel/poll.c
+++ b/kernel/poll.c
@@ -15,8 +15,9 @@ int poll(struct pollfd *fds, size_t nfds, int timeout) {
struct list *write_list = &get_current_task()->write_list;
struct list *disconnect_list = &get_current_task()->disconnect_list;
for (size_t i = 0; i < nfds; i++) {
- if (fds[i].fd < 0)
+ if (fds[i].fd < 0) {
continue;
+ }
vfs_fd_t *f = get_vfs_fd(fds[i].fd);
if (NULL == f) {
continue;
@@ -47,17 +48,22 @@ int poll(struct pollfd *fds, size_t nfds, int timeout) {
}
vfs_fd_t *f = get_vfs_fd(fds[i].fd);
if (!f) {
- if (fds[i].events & POLLHUP)
+ if (fds[i].events & POLLHUP) {
fds[i].revents |= POLLHUP;
+ }
} else {
- if (f->inode->has_data && fds[i].events & POLLIN)
+ if (f->inode->has_data && fds[i].events & POLLIN) {
fds[i].revents |= POLLIN;
- if (f->inode->can_write && fds[i].events & POLLOUT)
+ }
+ if (f->inode->can_write && fds[i].events & POLLOUT) {
fds[i].revents |= POLLOUT;
- if (!(f->inode->is_open) && fds[i].events & POLLHUP)
+ }
+ if (!(f->inode->is_open) && fds[i].events & POLLHUP) {
fds[i].revents |= POLLHUP;
- if (fds[i].revents)
+ }
+ if (fds[i].revents) {
rc++;
+ }
}
}
enable_interrupts();
diff --git a/kernel/random.c b/kernel/random.c
index 2bfd43b..bb28606 100644
--- a/kernel/random.c
+++ b/kernel/random.c
@@ -88,8 +88,9 @@ void add_hash_pool(void) {
u8 hash_buffer[HASH_LEN];
SHA1_Final(&hash_pool, hash_buffer);
- for (size_t i = 0; i < HASH_LEN; i++)
+ for (size_t i = 0; i < HASH_LEN; i++) {
new_chacha_key[i % KEY_SIZE] ^= hash_buffer[i];
+ }
SHA1_Init(&hash_pool);
SHA1_Update(&hash_pool, hash_buffer, HASH_LEN);
@@ -106,8 +107,9 @@ void add_hash_pool(void) {
void add_entropy(u8 *buffer, size_t size) {
SHA1_Update(&hash_pool, buffer, size);
hash_pool_size += size;
- if (hash_pool_size >= HASH_LEN * 2)
+ if (hash_pool_size >= HASH_LEN * 2) {
add_hash_pool();
+ }
}
void setup_random(void) {
diff --git a/kernel/sched/scheduler.c b/kernel/sched/scheduler.c
index 5a95c4c..1aae1ce 100644
--- a/kernel/sched/scheduler.c
+++ b/kernel/sched/scheduler.c
@@ -81,10 +81,12 @@ bool get_task_from_pid(u32 pid, process_t **out) {
}
void set_signal_handler(int sig, void (*handler)(int)) {
- if (sig >= 20 || sig < 0)
+ if (sig >= 20 || sig < 0) {
return;
- if (9 == sig)
+ }
+ if (9 == sig) {
return;
+ }
current_task->signal_handlers[sig] = handler;
}
@@ -93,28 +95,18 @@ void insert_eip_on_stack(u32 cr3, u32 address, u32 value);
process_t *create_process(process_t *p, u32 esp, u32 eip) {
process_t *r;
r = kcalloc(1, sizeof(process_t));
- r->dead = 0;
r->pid = next_pid++;
- r->esp = r->ebp = 0;
- r->eip = 0;
- r->sleep_until = 0;
- r->is_interrupted = 0;
- r->is_halted = 0;
if (!p) {
assert(1 == next_pid);
strncpy(r->program_name, "[kernel]", sizeof(current_task->program_name));
- } else
+ } else {
strncpy(r->program_name, "[Not yet named]",
sizeof(current_task->program_name));
+ }
r->cr3 = (p) ? clone_directory(get_active_pagedirectory())
: get_active_pagedirectory();
- r->next = 0;
- r->incoming_signal = 0;
r->parent = p;
- r->child = NULL;
- r->halt_list = NULL;
- r->interrupt_handler = NULL;
r->tcb = kcalloc(1, sizeof(struct TCB));
r->tcb->CR3 = r->cr3->physical_address;
@@ -139,17 +131,12 @@ process_t *create_process(process_t *p, u32 esp, u32 eip) {
r->tcb->ESP = esp;
}
- mmu_allocate_region((void *)(0x80000000 - 0x8000), 0x8000, MMU_FLAG_RW,
- r->cr3);
- r->signal_handler_stack = 0x80000000;
-
if (p) {
strcpy(r->current_working_directory, p->current_working_directory);
} else {
strcpy(r->current_working_directory, "/");
}
r->data_segment_end = (p) ? p->data_segment_end : NULL;
- memset((void *)r->halts, 0, 2 * sizeof(u32));
for (int i = 0; i < 100; i++) {
if (p) {
r->file_descriptors[i] = p->file_descriptors[i];
@@ -164,14 +151,18 @@ process_t *create_process(process_t *p, u32 esp, u32 eip) {
}
int get_free_fd(process_t *p, int allocate) {
- if (!p)
+ if (!p) {
p = (process_t *)current_task;
+ }
int i;
- for (i = 0; i < 100; i++)
- if (!p->file_descriptors[i])
+ for (i = 0; i < 100; i++) {
+ if (!p->file_descriptors[i]) {
break;
- if (p->file_descriptors[i])
+ }
+ }
+ if (p->file_descriptors[i]) {
return -1;
+ }
if (allocate) {
vfs_fd_t *fd = p->file_descriptors[i] = kmalloc(sizeof(vfs_fd_t));
fd->inode = kmalloc(sizeof(vfs_inode_t));
@@ -187,8 +178,9 @@ void tasking_init(void) {
int i = 0;
void free_process(void) {
- kprintf("pid: %x\n", get_current_task()->pid);
- kprintf("Exiting process: %s\n", get_current_task()->program_name);
+ process_t *p = get_current_task();
+ kprintf("pid: %x\n", p->pid);
+ kprintf("Exiting process: %s\n", p->program_name);
// free_process() will purge all contents such as allocated frames
// out of the current process. This will be called by exit() and
// exec*().
@@ -197,14 +189,19 @@ void free_process(void) {
// underlying frames as "unused".
for (int i = 0; i < 100; i++) {
vfs_close(i);
- if (!current_task->maps[i])
+ if (!current_task->maps[i]) {
continue;
+ }
MemoryMap *m = current_task->maps[i];
mmu_remove_virtual_physical_address_mapping(m->u_address, m->length);
}
// NOTE: Kernel stuff begins at 0x90000000
mmu_free_address_range((void *)0x1000, 0x90000000);
+
+ list_free(&p->read_list);
+ list_free(&p->write_list);
+ list_free(&p->disconnect_list);
}
void exit(int status) {
@@ -215,8 +212,9 @@ void exit(int status) {
}
process_t *new_task = current_task;
for (; new_task == current_task;) {
- if (!new_task->next)
+ if (!new_task->next) {
new_task = ready_queue;
+ }
new_task = new_task->next;
}
@@ -314,8 +312,9 @@ int fork(void) {
}
process_t *tmp_task = (process_t *)ready_queue;
- for (; tmp_task->next;)
+ for (; tmp_task->next;) {
tmp_task = tmp_task->next;
+ }
tmp_task->next = new_task;
@@ -326,9 +325,11 @@ int fork(void) {
}
int is_halted(process_t *process) {
- for (int i = 0; i < 2; i++)
- if (process->halts[i])
+ for (int i = 0; i < 2; i++) {
+ if (process->halts[i]) {
return 1;
+ }
+ }
if (isset_fdhalt(process)) {
return 1;
@@ -339,24 +340,17 @@ int is_halted(process_t *process) {
extern PageDirectory *active_directory;
process_t *next_task(process_t *c) {
- process_t *s = c;
- int loop = 0;
for (;;) {
- if (s == c) {
- if (1 == loop) {
- return s;
- }
- // loop = 1;
- }
c = c->next;
- if (!c)
+ if (!c) {
c = ready_queue;
- if (c->incoming_signal)
- break;
- if (c->sleep_until > pit_num_ms())
+ }
+ if (c->sleep_until > pit_num_ms()) {
continue;
- if (is_halted(c) || c->dead)
+ }
+ if (is_halted(c) || c->dead) {
continue;
+ }
break;
}
return c;
@@ -365,18 +359,21 @@ process_t *next_task(process_t *c) {
int kill(pid_t pid, int sig) {
process_t *p = current_task;
p = p->next;
- if (!p)
+ if (!p) {
p = ready_queue;
+ }
for (; p->pid != pid;) {
- if (p == current_task)
+ if (p == current_task) {
break;
+ }
p = p->next;
- if (!p)
+ if (!p) {
p = ready_queue;
+ }
}
- if (p->pid != pid)
+ if (p->pid != pid) {
return -ESRCH;
- p->incoming_signal = sig;
+ }
return 0;
}
@@ -393,9 +390,11 @@ void switch_task() {
}
MemoryMap **get_free_map(void) {
- for (int i = 0; i < 100; i++)
- if (!(current_task->maps[i]))
+ for (int i = 0; i < 100; i++) {
+ if (!(current_task->maps[i])) {
return &(current_task->maps[i]);
+ }
+ }
assert(0);
return NULL;
}
@@ -413,8 +412,9 @@ void *allocate_virtual_user_memory(size_t length, int prot, int flags) {
(void)prot;
(void)flags;
void *rc = get_free_virtual_memory(length);
- if ((void *)-1 == rc)
+ if ((void *)-1 == rc) {
return (void *)-1;
+ }
mmu_allocate_region(rc, length, MMU_FLAG_RW, NULL);
return rc;
@@ -422,8 +422,9 @@ void *allocate_virtual_user_memory(size_t length, int prot, int flags) {
void *user_kernel_mapping(void *kernel_addr, size_t length) {
void *rc = get_free_virtual_memory(length);
- if ((void *)-1 == rc)
+ if ((void *)-1 == rc) {
return (void *)-1;
+ }
mmu_map_directories(rc, NULL, kernel_addr, NULL, length);
return rc;
@@ -431,8 +432,9 @@ void *user_kernel_mapping(void *kernel_addr, size_t length) {
void *create_physical_mapping(void **physical_addresses, size_t length) {
void *rc = get_free_virtual_memory(length);
- if ((void *)-1 == rc)
+ if ((void *)-1 == rc) {
return (void *)-1;
+ }
int n = (uintptr_t)align_page((void *)length) / 0x1000;
for (int i = 0; i < n; i++) {
mmu_map_physical(rc + (i * 0x1000), NULL, physical_addresses[i], 0x1000);
@@ -481,8 +483,9 @@ void *mmap(void *addr, size_t length, int prot, int flags, int fd,
// code.
}
- if (length > vmobject->size)
+ if (length > vmobject->size) {
length = vmobject->size;
+ }
void *rc = create_physical_mapping(vmobject->object, length);
free_map->u_address = rc;
free_map->k_address = NULL;
diff --git a/kernel/sched/scheduler.h b/kernel/sched/scheduler.h
index f185e0f..596bedb 100644
--- a/kernel/sched/scheduler.h
+++ b/kernel/sched/scheduler.h
@@ -54,11 +54,6 @@ struct Process {
u32 pid;
char program_name[100];
char current_working_directory[MAX_PATH];
- u32 eip, esp, ebp;
- u32 saved_eip, saved_esp, saved_ebp;
- u32 useresp;
- u8 incoming_signal;
- u32 signal_handler_stack;
void *signal_handlers[20];
void *interrupt_handler;
PageDirectory *cr3;
diff --git a/kernel/socket.c b/kernel/socket.c
index ceed435..0f960f5 100644
--- a/kernel/socket.c
+++ b/kernel/socket.c
@@ -93,8 +93,9 @@ handle_incoming_tcp_connection(u8 ip[4], u16 n_port, u16 dst_port) {
int i;
for (i = 0; i < 100; i++) {
- if (!tcp_connections[i].is_used)
+ if (!tcp_connections[i].is_used) {
break;
+ }
}
tcp_connections[i].is_used = 1;
diff --git a/kernel/syscalls/isatty.c b/kernel/syscalls/isatty.c
index 883886e..9f3cca1 100644
--- a/kernel/syscalls/isatty.c
+++ b/kernel/syscalls/isatty.c
@@ -4,9 +4,11 @@
int syscall_isatty(int fd) {
vfs_fd_t *fd_ptr = get_vfs_fd(fd);
- if (!fd_ptr)
+ if (!fd_ptr) {
return -EBADF;
- if (!fd_ptr->is_tty)
+ }
+ if (!fd_ptr->is_tty) {
return -ENOTTY;
+ }
return 1;
}
diff --git a/kernel/syscalls/recvfrom.c b/kernel/syscalls/recvfrom.c
index ecc9fdf..c35dcfa 100644
--- a/kernel/syscalls/recvfrom.c
+++ b/kernel/syscalls/recvfrom.c
@@ -24,8 +24,9 @@ size_t syscall_recvfrom(
u16 data_length;
socklen_t tmp_socklen;
vfs_pread(socket, &tmp_socklen, sizeof(socklen_t), 0);
- if (address_len)
+ if (address_len) {
*address_len = tmp_socklen;
+ }
if (address) {
vfs_pread(socket, address, tmp_socklen, 0);
} else {
diff --git a/kernel/syscalls/stat.c b/kernel/syscalls/stat.c
index 0af2121..83ad3f0 100644
--- a/kernel/syscalls/stat.c
+++ b/kernel/syscalls/stat.c
@@ -6,8 +6,9 @@ int syscall_stat(SYS_STAT_PARAMS *args) {
const char *pathname = copy_and_allocate_user_string(args->pathname);
struct stat *statbuf = args->statbuf;
int fd = vfs_open(pathname, O_READ, 0);
- if (0 > fd)
+ if (0 > fd) {
return -ENOENT;
+ }
int rc = vfs_fstat(fd, statbuf);
vfs_close(fd);
return rc;
diff --git a/kernel/syscalls/write.c b/kernel/syscalls/write.c
index ef8a46f..80c6e57 100644
--- a/kernel/syscalls/write.c
+++ b/kernel/syscalls/write.c
@@ -4,8 +4,9 @@
int syscall_write(int fd, const char *buf, size_t count) {
vfs_fd_t *fd_ptr = get_vfs_fd(fd);
- if (!fd_ptr)
+ if (!fd_ptr) {
return -EBADF;
+ }
int rc = vfs_pwrite(fd, (char *)buf, count, fd_ptr->offset);
fd_ptr->offset += rc;
return rc;