Skip to content
This repository has been archived by the owner on Jan 23, 2025. It is now read-only.

fixed error #88

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added build/37_Anders-Sindre/disk.iso
Binary file not shown.
Binary file added build/37_Anders-Sindre/kernel.bin
Binary file not shown.
Binary file added build/37_Anders-Sindre/kernel.iso
Binary file not shown.
151 changes: 151 additions & 0 deletions src/37_Anders-Sindre/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
########################################
# The University of Agder Operating System: UiAOS
# Languages: C, C++, and NASM Assembly
# Tip: Use Ctrl+Shift+P in Visual Studio Code to get started with CMake.
########################################

# Skip compiler self-tests (saves time, avoids errors with some cross compilers)
set(CMAKE_C_COMPILER_WORKS 1)
set(CMAKE_CXX_COMPILER_WORKS 1)

# Minimum required CMake version
cmake_minimum_required(VERSION 3.22.1)

# Project name and languages used
project(UiAOS LANGUAGES C CXX ASM_NASM)

# Create a lock file to prevent parallel runs of CMake
file(LOCK ${CMAKE_SOURCE_DIR} DIRECTORY GUARD FILE)

########################################
# CMake: Import Plugins
########################################
include(FetchContent)

########################################
# UiAOS: Variables
########################################
set(OS_ARCH_TARGET "i386") # x86_64
set(OS_NAME "UiA Operating System")
set(OS_KERNEL_NAME "uiaos")
set(OS_KERNEL_BINARY "kernel.bin")
set(OS_KERNEL_IMAGE "kernel.iso")

########################################
# Compiler Configuration
########################################
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_C_STANDARD 99)

########################################
# Assembly Configuration
########################################
set(CMAKE_ASM_NASM_SOURCE_FILE_EXTENSIONS "s;S;asm")
if(OS_ARCH_TARGET STREQUAL "i386")
set(CMAKE_ASM_NASM_OBJECT_FORMAT "elf32")
elseif(OS_ARCH_TARGET STREQUAL "x86_64")
set(CMAKE_ASM_NASM_OBJECT_FORMAT "elf64")
endif()

# Command to compile NASM files
set(CMAKE_ASM_NASM_COMPILE_OBJECT
"<CMAKE_ASM_NASM_COMPILER> <DEFINES> <INCLUDES> <FLAGS> -f ${CMAKE_ASM_NASM_OBJECT_FORMAT} -o <OBJECT> <SOURCE>")

########################################
# OS Target
########################################
set(OS_KERNEL_LINKER "${CMAKE_CURRENT_SOURCE_DIR}/src/arch/${OS_ARCH_TARGET}/linker.ld")

# Add executable target for the kernel
add_executable(uiaos-kernel
src/multiboot2.asm # TODO: Add multiboot2 support
src/kernel.c
src/kernel.cpp
src/libc/string.c
src/libc/stdio.c
src/gdt.c
src/description_tables.asm
src/isr_asm.asm
src/monitor.c
src/common.c
src/idt.c
src/irq.c
src/irs.c
src/input.c
src/isr/mouse_isr.c
)


# Include directories for the kernel target
target_include_directories(uiaos-kernel PUBLIC include)


# Specify compile options for C and C++
target_compile_options(uiaos-kernel PRIVATE
$<$<COMPILE_LANGUAGE:C>:-Wall -Wextra -nostdinc -nostdlib -fno-builtin -fno-stack-protector -fno-stack-check -fno-lto -fPIE -m32 -march=i386 -mno-mmx -mno-sse -mno-sse2 -mno-red-zone -Wno-main -g>
$<$<COMPILE_LANGUAGE:CXX>:-Wall -Wextra -nostdinc -nostdlib -fno-builtin -fno-stack-protector -fno-stack-check -fno-lto -fPIE -m32 -march=i386 -mno-mmx -mno-sse -mno-sse2 -mno-red-zone -g>
$<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-m32 -march=i386 -Wno-unused-variable -Wno-unused-parameter>
)


# Specify link options for C and C++
target_link_options(uiaos-kernel PUBLIC
$<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-ffreestanding -nostdlib -fno-builtin -static -pie -O0 -T${OS_KERNEL_LINKER} -g>
)


target_link_options(
uiaos-kernel
PUBLIC "-ffreestanding"
PUBLIC "-nostdlib"
PUBLIC "-static"
PUBLIC "-pie"
PUBLIC "-T${OS_KERNEL_LINKER}"
)


# Set properties for the kernel target
set_target_properties(uiaos-kernel PROPERTIES
OUTPUT_NAME "${OS_KERNEL_BINARY}"
#LINK_FLAGS "${OS_KERNEL_LINK_FLAGS}"
)

########################################
# Create Empty Fat32 Disk Image
########################################
set(DISK_IMAGE "${CMAKE_CURRENT_BINARY_DIR}/disk.iso")

# Custom target to create an empty FAT32 disk image of 32MB
add_custom_target(
create-fat32-disk
COMMAND dd if=/dev/zero of=${DISK_IMAGE} bs=1M count=32
COMMAND mkfs.fat -F 32 ${DISK_IMAGE}
VERBATIM
)

########################################
# OS-Image Target
########################################
set(ISO_DIR ${CMAKE_CURRENT_BINARY_DIR}/iso)
set(LIMINE_CONFIG_DIR ${CMAKE_SOURCE_DIR})
set(LIMINE_DIR /usr/local/limine)
add_custom_target(
uiaos-create-image
COMMAND rm -rf ${ISO_DIR}
COMMAND mkdir -p ${ISO_DIR}
COMMAND cp -v $<TARGET_FILE:uiaos-kernel>
${LIMINE_CONFIG_DIR}/limine.cfg ${LIMINE_DIR}/limine-bios.sys ${LIMINE_DIR}/limine-bios-cd.bin
${LIMINE_DIR}/limine-uefi-cd.bin ${ISO_DIR}/
COMMAND mkdir -p ${ISO_DIR}/EFI/BOOT
COMMAND cp -v ${LIMINE_DIR}/BOOTX64.EFI ${ISO_DIR}/EFI/BOOT/
COMMAND cp -v ${LIMINE_DIR}/BOOTIA32.EFI ${ISO_DIR}/EFI/BOOT/
COMMAND xorriso -as mkisofs -b limine-bios-cd.bin
-no-emul-boot -boot-load-size 4 -boot-info-table
--efi-boot limine-uefi-cd.bin
-efi-boot-part --efi-boot-image --protective-msdos-label
${ISO_DIR} -o ${CMAKE_CURRENT_BINARY_DIR}/kernel.iso
COMMAND ${LIMINE_DIR}/limine bios-install ${CMAKE_CURRENT_BINARY_DIR}/kernel.iso
#COMMAND sudo rm -rf ${ISO_DIR}
DEPENDS create-fat32-disk uiaos-kernel
VERBATIM
)
Empty file added src/37_Anders-Sindre/README.md
Empty file.
9 changes: 9 additions & 0 deletions src/37_Anders-Sindre/include/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef COMMON_H
#define COMMON_H

#include "libc/stdint.h"

void outb(uint16_t port, uint8_t value);
uint8_t inb(uint16_t port);

#endif
55 changes: 55 additions & 0 deletions src/37_Anders-Sindre/include/descriptor_tables.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#ifndef DESCRIPTOR_TABLES_H
#define DESCRIPTOR_TABLES_H

#include "libc/stdint.h"

// How many entryes we want in the gdt array
#define GDT_ENTRIES 5
// How many entryes we want in the idt array
#define IDT_ENTRIES 256

struct gdt_entry {
uint16_t limit_low;
uint16_t base_low;
uint8_t base_middle;
uint8_t access;
uint8_t granularity;
uint8_t base_high;
} __attribute__((packed));

struct idt_entry {
uint16_t base_low;
uint16_t selector;
uint8_t zero;
uint8_t flags;
uint16_t base_high;
} __attribute__((packed));

struct gdt_ptr {
uint16_t limit;
uint32_t base;
} __attribute__((packed));

struct idt_ptr {
uint16_t limit;
uint32_t base;
} __attribute__((packed));

void init_gdt();
void init_idt();

void gdt_load(struct gdt_ptr *gdt_ptr);
void idt_load();

void gdt_set_gate(int32_t num, uint32_t base, uint32_t limit, uint8_t access, uint8_t gran);



static struct idt_entry idt[IDT_ENTRIES];
static struct idt_ptr idt_ptr;
// Makes an array based on gdt_entry
static struct gdt_entry gdt[GDT_ENTRIES];
// makes a instance of gdt_ptr
static struct gdt_ptr gdt_ptr;

#endif
3 changes: 3 additions & 0 deletions src/37_Anders-Sindre/include/input.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

char scancode_to_ascii(unsigned char* scan_code);
141 changes: 141 additions & 0 deletions src/37_Anders-Sindre/include/interrupts.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#ifndef INTERRUPTS_H
#define INTERRUPTS_H

#include "libc/stdint.h"
#include "descriptor_tables.h"

#define ISR1 1
#define ISR2 2
#define ISR3 3
#define ISR4 4
#define ISR5 5
#define ISR6 6
#define ISR7 7
#define ISR8 8
#define ISR9 9
#define ISR10 10
#define ISR11 11
#define ISR12 12
#define ISR13 13
#define ISR14 14
#define ISR15 15
#define ISR16 16
#define ISR17 17
#define ISR18 18
#define ISR19 19
#define ISR20 20
#define ISR21 21
#define ISR22 22
#define ISR23 23
#define ISR24 24
#define ISR25 25
#define ISR26 26
#define ISR27 27
#define ISR28 28
#define ISR29 29
#define ISR30 30
#define ISR31 31
#define IRQ0 32
#define IRQ1 33
#define IRQ2 34
#define IRQ3 35
#define IRQ4 36
#define IRQ5 37
#define IRQ6 38
#define IRQ7 39
#define IRQ8 40
#define IRQ9 41
#define IRQ10 42
#define IRQ11 43
#define IRQ12 44
#define IRQ13 45
#define IRQ14 46
#define IRQ15 47
#define IRQ_COUNT 16


extern void isr0 ();
extern void isr1 ();
extern void isr2 ();
extern void isr3 ();
extern void isr4 ();
extern void isr5 ();
extern void isr6 ();
extern void isr7 ();
extern void isr8 ();
extern void isr9 ();
extern void isr10();
extern void isr11();
extern void isr12();
extern void isr13();
extern void isr14();
extern void isr15();
extern void isr16();
extern void isr17();
extern void isr18();
extern void isr19();
extern void isr20();
extern void isr21();
extern void isr22();
extern void isr23();
extern void isr24();
extern void isr25();
extern void isr26();
extern void isr27();
extern void isr28();
extern void isr29();
extern void isr30();
extern void isr31();
extern void irq0 ();
extern void irq1 ();
extern void irq2 ();
extern void irq3 ();
extern void irq4 ();
extern void irq5 ();
extern void irq6 ();
extern void irq7 ();
extern void irq8 ();
extern void irq9 ();
extern void irq10();
extern void irq11();
extern void irq12();
extern void irq13();
extern void irq14();
extern void irq15();


void init_irq();
void init_interrupts();

typedef struct registers
{
uint32_t ds; // Data segment selector
uint32_t edi, esi, ebp, useless_value, ebx, edx, ecx, eax; // Pushed by pusha.
uint32_t int_no, err_code; // Interrupt number and error code (if applicable)
uint32_t eip, cs, eflags, esp, ss; // Pushed by the processor automatically.
} registers_t;


// Enables registration of callbacks for interrupts or IRQs.
// For IRQs, to ease confusion, use the #defines above as the
// first parameter.
typedef void (*isr_t)(registers_t*, void*);


// Structure to hold information about an interrupt handler
struct int_handler_t {
int num;
isr_t handler;
void *data;
};

// Define an interrupt handler
void register_irq_handler(int irq, isr_t handler, void* ctx);
void register_interrupt_handler(uint8_t n, isr_t handler, void*);


static struct int_handler_t int_handlers[IDT_ENTRIES];
static struct int_handler_t irq_handlers[IRQ_COUNT];


#endif
9 changes: 9 additions & 0 deletions src/37_Anders-Sindre/include/isr/mouse_isr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "libc/stdint.h"
#include "interrupts.h"

typedef struct {
int8_t x, y;
uint8_t left_button, right_button, middle_button;
} mouse_data_t;

void mouse_isr(registers_t* regs);
4 changes: 4 additions & 0 deletions src/37_Anders-Sindre/include/libc/limits.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once

#define INT_MAX 2147483647

14 changes: 14 additions & 0 deletions src/37_Anders-Sindre/include/libc/stdarg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

// va_list
typedef __builtin_va_list va_list;

// va_start
#define va_start(v, l) __builtin_va_start(v, l)

// va_end
#define va_end(v) __builtin_va_end(v)

// va_arg
#define va_arg(v, l) __builtin_va_arg(v, l)

Loading
Loading