Skip to content

Commit

Permalink
update unsigned data type with typedef uint*_t type
Browse files Browse the repository at this point in the history
  • Loading branch information
yzz127 committed Oct 31, 2019
1 parent 821e163 commit e471781
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 87 deletions.
18 changes: 9 additions & 9 deletions gdt.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@

typedef struct gdt_entry
{
unsigned short limit_low;
unsigned short base_low;
unsigned char base_middle;
unsigned char access;
unsigned char granularity;
unsigned char base_high;
uint16_t limit_low;
uint16_t base_low;
uint8_t base_middle;
uint8_t access;
uint8_t granularity;
uint8_t base_high;
} __attribute__((packed)) gdt_entry_t;

typedef struct gdt_ptr
{
unsigned short limit;
unsigned int base;
uint16_t limit;
uint32_t base;
} __attribute__ ((packed)) gdt_ptr_t;

gdt_entry_t gdt[5];
gdt_ptr_t gp;

extern void gdt_flush();

void gdt_set_gate(int num, unsigned long base, unsigned long limit, unsigned char access, unsigned char gran)
void gdt_set_gate(int num, unsigned long base, unsigned long limit, uint8_t access, uint8_t gran)
{
gdt[num].base_low = (base & 0xFFFF);
gdt[num].base_middle = (base >> 16) & 0xFF;
Expand Down
16 changes: 8 additions & 8 deletions idt.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@

typedef struct idt_entry
{
unsigned short base_lo;
unsigned short sel;
unsigned char always0;
unsigned char flags;
unsigned short base_hi;
uint16_t base_lo;
uint16_t sel;
uint8_t always0;
uint8_t flags;
uint16_t base_hi;
} __attribute__ ((packed)) idt_entry_t;

typedef struct idt_ptr
{
unsigned short limit;
unsigned int base;
uint16_t limit;
uint32_t base;
} __attribute__ ((packed)) idt_ptr_t;

idt_entry_t idt[256];
idt_ptr_t idtp;

extern void idt_load();

void idt_set_gate(unsigned char num, unsigned long base, unsigned short sel, unsigned char flags)
void idt_set_gate(uint8_t num, unsigned long base, uint16_t sel, uint8_t flags)
{
idt[num].base_lo = base & 0xFFFF;
idt[num].base_hi = (base >> 16) & 0xFFFF;
Expand Down
40 changes: 25 additions & 15 deletions include/system.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
#ifndef __SYSTEM_H
#define __SYSTEM_H

#define NULL 0

typedef signed char int8_t;
typedef short int16_t;
typedef long int32_t;
typedef long long int64_t;

typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned long uint32_t;
typedef unsigned long long uint64_t;

/* MAIN.C */
extern unsigned char *memecpy(unsigned char *dest, const unsigned char *src, int count);
extern unsigned char *emeset(unsigned char *dest, unsigned char val, int count);
extern unsigned short *memsetw(unsigned short *dest, unsigned short val, int count);
extern uint8_t *memcpy(uint8_t *dest, const uint8_t *src, int count);
extern uint8_t *memset(uint8_t *dest, uint8_t val, int count);
extern uint16_t *memsetw(uint16_t *dest, uint16_t val, int count);
extern int strlen(const char *str);
extern unsigned char inportb(unsigned short _port);
extern void outportb(unsigned short _port, unsigned char _data);
extern uint8_t inportb(uint16_t _port);
extern void outportb(uint16_t _port, uint8_t _data);

extern void cls();
extern void putch(unsigned char c);
extern void puts(unsigned char *str);
extern void settextcolor(unsigned char forecolor, unsigned char backcolor);
extern void putch(uint8_t c);
extern void puts(uint8_t *str);
extern void settextcolor(uint8_t forecolor, uint8_t backcolor);
extern void init_video();

extern void gdt_install();
Expand All @@ -21,20 +33,18 @@ extern void isrs_install();

typedef struct regs
{
unsigned int gs, fs, es, ds;
unsigned int edi, esi, ebp, esp, ebx, edx, ecx, eax;
unsigned int int_no, err_code;
unsigned int eip, cs, eflags, useresp, ss;
uint32_t gs, fs, es, ds;
uint32_t edi, esi, ebp, esp, ebx, edx, ecx, eax;
uint32_t int_no, err_code;
uint32_t eip, cs, eflags, useresp, ss;
} register_t;

void irq_install_handler(int irq, void (*handler)(register_t *r));
void irq_uninstall_handler(int irq);
void irq_install();

void timer_install(unsigned int frequency);
void timer_install(uint32_t frequency);

void keyborad_install();

#define NULL 0

#endif
2 changes: 1 addition & 1 deletion isrs.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void isrs_install()
idt_set_gate(31, (unsigned)_isr31, 0x08, 0x8E);
}

unsigned char *exception_messages[] =
uint8_t *exception_messages[] =
{
"Division By Zero",
"Debug",
Expand Down
4 changes: 2 additions & 2 deletions kb.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <system.h>

unsigned char kbdus[128] =
uint8_t kbdus[128] =
{
0, 27, '1', '2', '3', '4', '5', '6', '7', '8', /* 9 */
'9', '0', '-', '=', '\b', /* Backspace */
Expand Down Expand Up @@ -42,7 +42,7 @@ unsigned char kbdus[128] =

void keyboard_handler(register_t *r)
{
unsigned char scancode;
uint8_t scancode;

scancode = inportb(0x60);

Expand Down
12 changes: 6 additions & 6 deletions kheap.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <system.h>

unsigned int kmalloc_internel(unsigned int size, int align, unsigned int *phys)
uint32_t kmalloc_internel(uint32_t size, int align, uint32_t *phys)
{
if (align == 1 && (start_address & 0xFFFFF000))
{
Expand All @@ -11,26 +11,26 @@ unsigned int kmalloc_internel(unsigned int size, int align, unsigned int *phys)
{
*phys = start_address;
}
unsigned int tmp = start_address;
uint32_t tmp = start_address;
start_address += size;
return tmp;
}

unsigned int kmalloc_a(unsigned int size) {
uint32_t kmalloc_a(uint32_t size) {
return kmalloc_internel(size, 1, NULL);
}

unsigned int kmalloc_p(unsigned int size, unsigned int *phys)
uint32_t kmalloc_p(uint32_t size, uint32_t *phys)
{
return kmalloc_internel(size, 0, phys);
}

unsigned int kmalloc_ap(unsigned int size, unsigned int *phys)
uint32_t kmalloc_ap(uint32_t size, uint32_t *phys)
{
return kmalloc_internel(size, 1, phys);
}

unsigned int kmalloc(unsigned int size)
uint32_t kmalloc(uint32_t size)
{
return kmalloc_internel(size, 0, NULL);
}
14 changes: 7 additions & 7 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <system.h>

unsigned char *memcpy(unsigned char *dest, const unsigned char *src, int count)
uint8_t *memcpy(uint8_t *dest, const uint8_t *src, int count)
{
const char *sp = (const char *)src;
char *dp = (char *)dest;
Expand All @@ -12,7 +12,7 @@ unsigned char *memcpy(unsigned char *dest, const unsigned char *src, int count)

}

unsigned char *memset(unsigned char *dest, unsigned char val, int count)
uint8_t *memset(uint8_t *dest, uint8_t val, int count)
{
char *temp = (char *)dest;
while (count > 0) {
Expand All @@ -22,9 +22,9 @@ unsigned char *memset(unsigned char *dest, unsigned char val, int count)
return dest;
}

unsigned short *memsetw(unsigned short *dest, unsigned short val, int count)
uint16_t *memsetw(uint16_t *dest, uint16_t val, int count)
{
unsigned short *temp = (unsigned short *)dest;
uint16_t *temp = (uint16_t *)dest;
while (count > 0) {
*temp++ = val;
count--;
Expand All @@ -40,14 +40,14 @@ int strlen(const char *str)
return retval;
}

unsigned char inportb(unsigned short _port)
uint8_t inportb(uint16_t _port)
{
unsigned char rv;
uint8_t rv;
__asm__ __volatile__("inb %1, %0" : "=a" (rv) : "dN" (_port));
return rv;
}

void outportb(unsigned short _port, unsigned char _data)
void outportb(uint16_t _port, uint8_t _data)
{
__asm__ __volatile__("outb %1, %0" : : "dN" (_port), "a" (_data));
}
Expand Down
77 changes: 58 additions & 19 deletions paging.c
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
#include <paging.h>

unsigned int *frames;
unsigned int nframes;
uint32_t *frames;
uint32_t nframes;

extern unsigned int start_address;
extern uint32_t start_address;

#define INDEX_FROM_BIT(a) (a / (8 * 4))
#define OFFSET_FROM_BIT(a) (a % (8 * 4))

static void set_frame(unsigned int frame_addr)
static void set_frame(uint32_t frame_addr)
{
unsigned int frame = frame_addr / 0x1000;
unsigned int idx = INDEX_FROM_BIT(frame);
unsigned int off = OFFSET_FROM_BIT(frame);
uint32_t frame = frame_addr / 0x1000;
uint32_t idx = INDEX_FROM_BIT(frame);
uint32_t off = OFFSET_FROM_BIT(frame);
frames[idx] |= (0x1 << off);
}

static void clear_frame(unsigned frame_addr)
{
unsigned int frame = frame_addr / 0x1000;
unsigned int idx = INDEX_FROM_BIT(frame);
unsigned int off = OFFSET_FROM_BIT(frame);
uint32_t frame = frame_addr / 0x1000;
uint32_t idx = INDEX_FROM_BIT(frame);
uint32_t off = OFFSET_FROM_BIT(frame);
frames[idx] &= ~(0x1 << off);
}

static void test_frame(unsigned frame_addr)
{
unsigned int frame = frame_addr / 0x1000;
unsigned int idx = INDEX_FROM_BIT(frame);
unsigned int off = OFFSET_FROM_BIT(frame);
uint32_t frame = frame_addr / 0x1000;
uint32_t idx = INDEX_FROM_BIT(frame);
uint32_t off = OFFSET_FROM_BIT(frame);
return (frames[idx] & (0x1 << off));
}

static unsigned int first_frame()
static uint32_t first_frame()
{
unsigned int i, j;
uint32_t i, j;
for (i = 0; i < INDEX_FROM_BIT(nframes); i++)
{
if (frames[i] != 0xFFFFFFFF)
{
for (j = 0; j < 32; j++)
{
unsigned int toTest = 0x1 << j;
uint32_t toTest = 0x1 << j;
if (!(frames[i] & toTest))
{
return i * 4 * 8 + j;
Expand All @@ -59,8 +59,8 @@ void alloc_frame(page_t *page, int is_kernel, int is_writeable)
}
else
{
unsigned int idx = first_frame();
if (idx == (unsigned int)-1)
uint32_t idx = first_frame();
if (idx == (uint32_t)-1)
{
PANIC("No free frames!");
}
Expand All @@ -75,7 +75,7 @@ void alloc_frame(page_t *page, int is_kernel, int is_writeable)

void free_frame(page_t *page)
{
unsigned int frame;
uint32_t frame;
if (!(frame = page->frame))
{
return;
Expand All @@ -85,4 +85,43 @@ void free_frame(page_t *page)
clear_frame(frame);
page->frame = 0x0;
}
}

void init_paging()
{
uint32_t mem_end_page = 0x1000000; // The size of the physical memory is assumed to be 16MB

nframes = mem_end_page / 0x1000;
frames = (uint32_t *)kmalloc(INDEX_FROM_BIT(nframes));
memset(frames, 0, INDEX_FROM_BIT(nframes));

kernel_directory = (page_directory_t *)kmalloc_a(sizeof(page_directory_t));
memset(kernel_directory, 0, sizeof(page_directory_t));
current_directory = kernel_directory;

int i = 0;
while (i < start_address)
{
alloc_frame(get_page(i, 1, kernel_directory), 0, 0);
i += 0x1000;
}

irq_install_handler(14, page_fault);

swict_page_directory(kernel_directory);
}

void switch_page_directory(page_directory_t *dir)
{
current_directory = dir;
asm volatile("mov %0, %%cr3":: "r"(&dir->tablesPhysical));
uint32_t cr0;
asm volatile("mov %%cr0, %0": "=r"(cr0));
cr0 |= 0x80000000; // Enable paging
asm volatile("mov %0, %%cr0":: "r"(cr0));
}

page_t *get_page(uint32_t address, int make, page_directory_t *dir)
{

}
Loading

0 comments on commit e471781

Please sign in to comment.