Skip to content

Commit

Permalink
kfree() implemented
Browse files Browse the repository at this point in the history
rewrite some of the components related to the global descriptor table
  • Loading branch information
akhal3d96 committed Feb 15, 2019
1 parent f5bb997 commit a64aa39
Show file tree
Hide file tree
Showing 12 changed files with 199 additions and 118 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ before_deploy:
deploy:
provider: releases
prerelease: true
skip_cleanup: true
api_key:
secure: pZs/aomPz8qbz+ubo/jYDST8yFO5DktkTwwoPlB7xKCmdzvGo8ft+F9xKf06OvOEFtRcSzNUmSuYl88jPWbhXDZVqwpaSj4HeuH6VtpPaRpeocAYpYUB6MRphKQT+U34JJ/ybPkmRl4jvVQ5KyMSVF/2r9GvL1mBEhAgRVxuTKPWfCHillyNvFMCCXBEiSsvJQjbFXP1Oi4dFewyIf+fIvXxdeJDae8PzI/VE6kwCEqmC3cdbeu+beyM0ewxQR4DF6okEAUKtl+/LO2+MKa8oMKt7gzeJz6mWlrhyJKbLvL1zHRYyfJM9eyYoojg9ybB8Ye3LqvvbfVdl4q0BMUG4WWAiFyjs59qV6mITdTQw3udopcfP2h9kaNaMRZMmMEjApG06V65poJcfzxeTt07Pui45L4P1vhKzRQIbUiPcHdue8Hq0MTF5X7sKNpJC6Yzb9LMKPA2eegRRLg9lLKRfRhO8PQnjcrc1yNSdlskKRFqsWr9osJ7QyC++EKX3UmNjW1zY5bcTT4tfid6aA039PTYInw2fZ9i3WXoUz6fROME13KUuJJ92R9lMnk42PxDcwJBg7qo8aLQ3OUzw8vkQT3vxvLGnNTl4dNRlVPzO6EQ9J3iZtXW1ctH4rsTlQwqBJrs01M2Jf+45znYLsOaZThnEKgsc39h1eCi9FjdNGo=
file: disk.img
file: kawaiios.iso
on:
repo: nemoload/KawaiiOS
56 changes: 37 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,51 @@
# KawaiiOS
[![Build Status](https://travis-ci.com/nemoload/KawaiiOS.svg?branch=master)](https://travis-ci.com/nemoload/KawaiiOS)

## What is KawaiiOS?
KwaiiOS is a hobby A 32-bit operating system for educational purposes meant to be UNIX-like but with its own kernel. It's tested on an emulation of Intel Q35 chipset.
KwaiiOS is a hobbyist 32-bit, uniprocessor, operating system made for educational purposes. Its final goal is to be a UNIX-like operating system. It's tested on an Intel Q35 chipset virtual machine.

<p align="center">
<img src="https://raw.githubusercontent.com/nemoload/KawaiiOS/master/screenshots/screenshot1.png">
</p>

## Memory map
* ### Physical memory
| 0x00000 | 0x07C00 | BIOS |
|--------- |---------- |------------------------ |
| 0x07C00 | 0x07E00 | Bootloader |
| 0x07E00 | 0x10000 | UNUSED |
| 0x10000 | 0x23CB8 | Kernel |
| 0x23CB8 | 0x100000 | UNUSED + system reserved |

## How to compile annd run it?
### Requirments:
1. A running GNU/Linux environment
2. GCC
3. GNU Make
4. QEMU
1. A GNU/Linux environment
2. GCC (with cross-compiling)
3. NASM
4. GNU Make
5. QEMU.
6. cdrtools

### Installing:
`$ sudo apt-get install nasm mkisofs gcc-multilib`
### Compiling:
`$ make # Haven't expected that, have you?`
### Running:
`$ qemu-system-i386 -enable-kvm -d cpu_reset -boot d -cdrom kawaiios.iso -m 16`

You can omitt the `-enable-kvm` parameter if you don't have/don't want to. Also the memory size is, for now, hard coded into the source code so changing the memory size from **16** in `-m 16` to anything else migh break up some stuff.

## Features:

#### Supported Archetictures:
- [x] Intel x86
- [ ] ARMv7-A


Archticture-depndent features:
- [ ] Multitasking
- [ ] Multithreading

#### Kernel Features:
- [x] Monolitich design
- [ ] Virtual File System

#### Supported Devices:

- [x] PS/2 Keyboard


### Compiling
`$ make`

This will compile the bootloader and the kernel, then it'll link the kernel components and finally will genrate a floppy disk image out of both the botloader and the kernel.
### Running
`$ qemu-system-i386 -machine q35 -fda disk.img`
## Note
The code of KawaiiOS is far from ready there's a lot of things that should be fixed and changed and the work is still in progress.
54 changes: 26 additions & 28 deletions kernel/descriptor_tables.c
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@

/* descriptor_tables.c - Initialises the GDT and IDT, and defines the */
/* default ISR and IRQ handler.*/
/* Based on code from Bran's kernel development tutorials.*/
/* Rewritten for JamesM's kernel development tutorials.*/

#include <asm.h>
#include <descriptor_tables.h>
#include <string.h>
#include <isr.h>

/* Lets us access our ASM functions from our C code.*/
extern void gdt_flush(uint32_t);
#define GDT_ENTRY_NUM 5

/* Assembly-defined functions */
extern void _asm_gdt_flush(uint32_t);
extern void idt_flush(uint32_t);

/* Internal function prototypes.*/
static void
gdt_set_gate(int32_t index, uint32_t base, uint32_t limit, uint8_t access,
uint8_t gran);
static void init_gdt();
static void init_idt();
static void gdt_set_gate(int32_t, uint32_t, uint32_t, uint8_t, uint8_t);
static void idt_set_gate(uint8_t, uint32_t, uint16_t, uint8_t);

gdt_entry_t gdt_entries[5];
gdt_entry_t gdt_entries[GDT_ENTRY_NUM];
gdt_ptr_t gdt_ptr;

idt_entry_t idt_entries[256];
idt_ptr_t idt_ptr;

Expand All @@ -31,7 +30,6 @@ extern isr_t interrupt_handlers[256];
/* initialises the GDT and IDT.*/
void init_descriptor_tables()
{

/* Initialise the global descriptor table. */
init_gdt();
/* Initialise the interrupt descriptor table. */
Expand All @@ -42,32 +40,32 @@ void init_descriptor_tables()

static void init_gdt()
{
gdt_ptr.limit = (sizeof(gdt_entry_t) * 5) - 1;
gdt_ptr.base = (uint32_t) & gdt_entries;
gdt_ptr.limit = (sizeof(gdt_entry_t) * GDT_ENTRY_NUM);
gdt_ptr.base = (uint32_t) &gdt_entries;

gdt_set_gate(0, 0, 0, 0, 0); /* Null segment */
gdt_set_gate(1, 0, 0xFFFFFFFF, 0x9A, 0xCF); /* Code segment */
gdt_set_gate(2, 0, 0xFFFFFFFF, 0x92, 0xCF); /* Data segment */
gdt_set_gate(3, 0, 0xFFFFFFFF, 0xFA, 0xCF); /* User mode code segment */
gdt_set_gate(4, 0, 0xFFFFFFFF, 0xF2, 0xCF); /* User mode data segment */

gdt_flush((uint32_t) & gdt_ptr);
_asm_gdt_flush((uint32_t) &gdt_ptr);
}

/* Set the value of one GDT entry.*/
static void
gdt_set_gate(int32_t num, uint32_t base, uint32_t limit, uint8_t access,
gdt_set_gate(int32_t index, uint32_t base, uint32_t limit, uint8_t access,
uint8_t gran)
{
gdt_entries[num].base_low = (base & 0xFFFF);
gdt_entries[num].base_middle = (base >> 16) & 0xFF;
gdt_entries[num].base_high = (base >> 24) & 0xFF;
gdt_entries[index].base_low = (base & 0xFFFF);
gdt_entries[index].base_middle = (base >> 16) & 0xFF;
gdt_entries[index].base_high = (base >> 24) & 0xFF;

gdt_entries[num].limit_low = (limit & 0xFFFF);
gdt_entries[num].granularity = (limit >> 16) & 0x0F;
gdt_entries[index].limit_low = (limit & 0xFFFF);
gdt_entries[index].granularity = (limit >> 16) & 0x0F;

gdt_entries[num].granularity |= gran & 0xF0;
gdt_entries[num].access = access;
gdt_entries[index].granularity |= gran & 0xF0;
gdt_entries[index].access = access;
}

static void init_idt()
Expand Down Expand Up @@ -143,14 +141,14 @@ static void init_idt()
}

static void
idt_set_gate(uint8_t num, uint32_t base, uint16_t sel, uint8_t flags)
idt_set_gate(uint8_t index, uint32_t base, uint16_t sel, uint8_t flags)
{
idt_entries[num].base_lo = base & 0xFFFF;
idt_entries[num].base_hi = (base >> 16) & 0xFFFF;
idt_entries[index].base_lo = base & 0xFFFF;
idt_entries[index].base_hi = (base >> 16) & 0xFFFF;

idt_entries[num].sel = sel;
idt_entries[num].always0 = 0;
idt_entries[index].sel = sel;
idt_entries[index].always0 = 0;
/* We must uncomment the OR below when we get to using user-mode. */
/* It sets the interrupt gate's privilege level to 3. */
idt_entries[num].flags = flags /* | 0x60 */ ;
idt_entries[index].flags = flags /* | 0x60 */ ;
}
16 changes: 7 additions & 9 deletions kernel/gdt.asm
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
;
; Gdt.s -- contains global descriptor table and interrupt descriptor table
; setup code.
; Based on code from Bran's kernel development tutorials.
; Rewritten for JamesM's kernel development tutorials.
[GLOBAL _asm_gdt_flush] ; Allows the C code to call _asm_gdt_flush().

[GLOBAL gdt_flush] ; Allows the C code to call gdt_flush().

gdt_flush:
mov eax, [esp+4] ; Get the pointer to the GDT, passed as a parameter.
_asm_gdt_flush:
push ebp
mov ebp, esp
mov eax, [ebp+8] ; Get the first argument.
lgdt [eax] ; Load the new GDT pointer

mov ax, 0x10 ; 0x10 is the offset in the GDT to our data segment
Expand All @@ -18,6 +14,8 @@ gdt_flush:
mov ss, ax
jmp 0x08:.flush ; 0x08 is the offset to our code segment: Far jump!
.flush:
mov esp, ebp
pop ebp
ret

[GLOBAL idt_flush] ; Allows the C code to call idt_flush().
Expand Down
43 changes: 23 additions & 20 deletions kernel/include/descriptor_tables.h
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
#ifndef COMMON_H
#define COMMON_H
#ifndef DESCRIPTOR_TABLES_H
#define DESCRIPTOR_TABLES_H

#include <asm.h>

/* Initialisation function is publicly accessible.*/
void init_descriptor_tables();

/* This structure contains the value of one GDT entry.*/
/* We use the attribute 'packed' to tell GCC not to change*/
/* any of the alignment in the structure.*/
struct gdt_entry_struct {
uint16_t limit_low; /* The lower 16 bits of the limit. */
uint16_t base_low; /* The lower 16 bits of the base. */
uint8_t base_middle; /* The next 8 bits of the base. */
uint8_t access; /* Access flags, determine what ring this segment can be used in. */
uint8_t granularity;
uint8_t base_high; /* The last 8 bits of the base. */
/*
* This structure contains the value of one GDT entry.
* We use the compiler attribute 'packed' to tell GCC
* not to change any of the alignment in the structure.
* Data based on Intel Vol3.A
*/
struct gdt_entry {
uint16_t limit_low; /* Segment Limit */
uint16_t base_low; /* Base Address */
uint8_t base_middle; /* Base */
uint8_t access; /* Type=4 | S=1 | DPL=2 | P=1 */
uint8_t granularity; /* SigLimit=4 | AVL=1 | L=1 | D/B=1 | G=1 */
uint8_t base_high; /* Base */
} __attribute__((packed));
typedef struct gdt_entry gdt_entry_t;

typedef struct gdt_entry_struct gdt_entry_t;

/* This struct describes a GDT pointer. It points to the start of*/
/* our array of GDT entries, and is in the format required by the*/
/* lgdt instruction.*/
struct gdt_ptr_struct {
/*
* This struct describes a GDT pointer. It points to the start of
* our array of GDT entries, and is in the format required by the
* `lgdt` instruction.
*/
struct gdt_ptr {
uint16_t limit; /* The upper 16 bits of all selector limits. */
uint32_t base; /* The address of the first gdt_entry_t struct. */
} __attribute__((packed));

typedef struct gdt_ptr_struct gdt_ptr_t;
typedef struct gdt_ptr gdt_ptr_t;

/* A struct describing an interrupt gate.*/
struct idt_entry_struct {
Expand Down
28 changes: 7 additions & 21 deletions kernel/include/mm/kheap.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,23 @@
#include <stdint.h>
#include <stdbool.h>

#include <utilis/bst.h>

#define KHEAP_MAGIC 0xBADDAD
#define KHEAP_END 0xFFBFF000
#define KHEAP_INITIAL_SIZE 0x100000
#define HEAP_INDEX_SIZE 0x20000
#define HEAP_MIN_SIZE 0x70000
#define KHEAP_START 0xCC0000
#define KHEAP_INDEX_SIZE 0x1000

/*#define HEAP_INDEX_SIZE 0x20000
#define HEAP_MIN_SIZE 0x70000*/

struct header {
uint32_t size;
bool is_hole;
struct header *next, *prev;
};

struct heap {
/*struct bst index; */
uint32_t start_address;
uint32_t end_address;

uint32_t max_address;

bool supervisor;
bool readonly;
};

struct heap *create_heap(uint32_t start_address, uint32_t end_address,
uint32_t max_address, bool supervisor, bool readonly);

void split_chunk(struct header *chunk, uint32_t size);
void allocate_chunk(uint32_t addr, uint32_t size);
void *kmalloc(uint32_t size);
void kfree(void *address, struct heap *heap);

void kfree(void *address);
void coalesce_block(struct header * block_header);
#endif
2 changes: 2 additions & 0 deletions kernel/include/mm/kmem.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ uint32_t kmalloc_a(uint32_t size);
uint32_t kmalloc_ap(uint32_t size, uint32_t * physical);

void allocate_frame(struct page *p, int is_kernel, int is_writeable);
void clear_frame(uint32_t frame_addr);

#endif
2 changes: 2 additions & 0 deletions kernel/include/string.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <stdint.h>

#define NULL (void *)0

void memcpy(void *dest, const void *src, uint32_t len);
void memset(void *dest, uint8_t val, uint32_t len);
int strcmp(char *str1, char *str2);
Expand Down
Loading

0 comments on commit a64aa39

Please # to comment.