Skip to content

Commit

Permalink
printf support added to make output and debugging easier. It outputs …
Browse files Browse the repository at this point in the history
…to an internal buffer and also outputs to VGA buffer based on a flag.
  • Loading branch information
shoily committed Nov 13, 2020
1 parent 7104fbb commit f834fb5
Show file tree
Hide file tree
Showing 12 changed files with 324 additions and 78 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ gcc -m32 -std=gnu99 -nostdlib -nostdinc -fno-builtin -fno-stack-protector -nosta
gcc -m32 -std=gnu99 -nostdlib -nostdinc -fno-builtin -fno-stack-protector -nostartfiles -nodefaultlibs -ffreestanding -fno-pic -Wall -Wextra -Werror -c page32.c -o page32.o<br>
gcc -m32 -std=gnu99 -nostdlib -nostdinc -fno-builtin -fno-stack-protector -nostartfiles -nodefaultlibs -ffreestanding -fno-pic -Wall -Wextra -Werror -c usermode.c -o usermode.o<br>
gcc -m32 -std=gnu99 -nostdlib -nostdinc -fno-builtin -fno-stack-protector -nostartfiles -nodefaultlibs -ffreestanding -fno-pic -Wall -Wextra -Werror -c lock.c -o lock.o<br>
gcc -m32 -std=gnu99 -nostdlib -nostdinc -fno-builtin -fno-stack-protector -nostartfiles -nodefaultlibs -ffreestanding -fno-pic -Wall -Wextra -Werror -c debug.c -o debug.o<br>

ld -static -T kernel32.ld -m elf_i386 -nostdlib --nmagic boot32.o util.o system.o apic.o smp.o setup32.o handlr32.o memory.o page32.o usermode.o lock.o start.o -o xiskernel.elf<br>
ld -static -T kernel32.ld -m elf_i386 -nostdlib --nmagic boot32.o util.o system.o apic.o smp.o setup32.o handlr32.o memory.o page32.o usermode.o lock.o debug.o start.o -o xiskernel.elf<br>
objcopy -O binary xiskernel.elf xiskernel.bin<br>

<b>Instructions for building boot loader -</b><br>
Expand Down
11 changes: 5 additions & 6 deletions apic.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "util.h"
#include "system.h"
#include "setup32.h"
#include "debug.h"

int lapic_present = 0;
int lapic_base_register;
Expand Down Expand Up @@ -81,7 +82,7 @@ void init_lapic() {
: "%eax", "%edx"
);

print_msg("Local APIC present", lapic_present, 10, true);
printf(KERNEL_INFO, "Local APIC present: %d\n", lapic_present);

if (!lapic_present) {

Expand All @@ -95,12 +96,12 @@ void init_lapic() {
lapic_base_register = eax & 0xfffff000;

build_pagetable((pgd_t*)&_kernel_pg_dir, pgtable, lapic_base_register, lapic_base_register, PAGE_SIZE, PGD_PRESENT | PGD_WRITE, PTE_PRESENT | PTE_WRITE);
print_msg("Local APIC address", eax, 16, false);

printf(KERNEL_INFO, "Local APIC address: %p\n", eax);

lapic_id = lapic_read_register(LAPIC_ID_REG) >> 24;

print_msg("Local APIC id", lapic_id, 16, true);
printf(KERNEL_INFO, "Local APIC id: %x\n", lapic_id);

// enable receiving interrupt
lapic_write_register(LAPIC_SPURIOUS_REG, lapic_read_register(LAPIC_SPURIOUS_REG)| 0x100);
Expand All @@ -118,7 +119,5 @@ void lapic_switch(bool enable) {
else
value &= ~0x1ff;

//print_msg("lapic_switch", value, 16, true);

lapic_write_register(LAPIC_SPURIOUS_REG, value);
}
157 changes: 157 additions & 0 deletions debug.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*****************************************************************************/
/* File: debug.c */
/* */
/* Description: Source file for debugging support. */
/* */
/* Author: Shoily O Rahman <[email protected]> */
/* */
/* Date: Nov 6, 2020 */
/* */
/*****************************************************************************/

#include "debug.h"
#include "lock.h"
#include "util.h"
#include "system.h"

char* cur_debug_mem;
spinlock lock_debug_mem;
int cur_offset;
extern int _kernel_debug_buffer;
int debug_level_for_vga;

void debug_init() {

cur_debug_mem = (char *)&_kernel_debug_buffer;
cur_offset = 0;
debug_level_for_vga = KERNEL_DEBUG;
INIT_SPIN_LOCK(&lock_debug_mem);
}

typedef enum _va_type {

VA_BYTE = 0,
VA_PERCENT = 1,
VA_CHAR = 2,
VA_UCHAR = 3,
VA_INT = 4,
VA_UINT = 5,
VA_HEX = 6,
VA_SHORT = 7,
VA_USHORT = 8
} va_type;

void printf(int level, char *fmt, ...) {

va_list list;
va_start(fmt, list);
char *p = fmt;
char *ptr;

union {
char c;
int i;
unsigned int u;
long long ll;
char *s;
void *p;
} val;
char str[20];
char *buf;

spinlock_lock(&lock_debug_mem);
buf = cur_debug_mem;

while(*p) {

if (*p == '%') {
p++;
if (*p == 37 || *p == '\0') {

*buf++ = 37;
//if (*p != '\0')
// p++;
} else {

if (*p == 'c') {

val.c = va_arg(list, char);
*buf++ = val.c;
} else if (*p == 'i' || *p == 'd' || *p == 'x') {

val.i = va_arg(list, int);
itoa(val.i, str, (*p == 'x') ? 16 : 10);
ptr = str;
while(*ptr) {
*buf++ = *ptr++;
}
} else if (*p == 'p') {

val.p = va_arg(list, void*);
ptrtoa(val.p, str, 16);
ptr = str;
while(*ptr) {
*buf++ = *ptr++;
}
} else if (*p == 'u') {

val.u = va_arg(list, unsigned int);
lltoa(val.u, str, 10);
ptr = str;
while(*ptr) {
*buf++ = *ptr++;
}
} else if (*p == 'l' && *(p+1) == 'l') {

p++;
val.ll = va_arg(list, long long);
lltoa(val.ll,str, 10);
ptr = str;
while(*ptr) {
*buf++ = *ptr++;
}
} else if (*p == 's') {

ptr = val.s = va_arg(list, char*);
while(*ptr) {
*buf++ = *ptr++;
}
}
}
} else {

*buf++ = *p;
}

p++;
}

*buf++ = '\0';

if (level >= debug_level_for_vga) {

print_vga(cur_debug_mem);
}

cur_debug_mem = buf;

spinlock_unlock(&lock_debug_mem);
}
/*
void debug_msg(int flags, char *msg, int len) {
int i = 0;
spinlock_lock(&lock_debug_mem);
while (i < len) {
*cur_debug_mem++ = msg[i++];
}
*cur_debug_mem++ = 0;
*cur_debug_mem++ = 0;
spinlock_unlock(&lock_debug_mem);
}
*/
25 changes: 25 additions & 0 deletions debug.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*****************************************************************************/
/* File: debug.h */
/* */
/* Description: Header file for debugging support. */
/* */
/* Author: Shoily O Rahman <[email protected]> */
/* */
/* Date: Nov 6, 2020 */
/* */
/*****************************************************************************/

#ifndef _DEBUG_H
#define _DEBUG_H

#define KERNEL_DEBUG 1
#define KERNEL_INFO 2
#define KERNEL_WARN 3
#define KERNEL_ERR 4
#define KERNEL_CRIT 5

void debug_init();
void printf(int level, char *fmt, ...);
//void debug_msg(char *msg, int len);

#endif
1 change: 1 addition & 0 deletions kernel32.ld
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ SECTIONS
_kernel_stack_0_start = _kernel_stack_0 + 0x2000;
_kernel_private_data = _kernel_stack_0_start + (0x2000 * 32);
_kernel_heap_start = (_kernel_private_data + 0x1000) & ~(0xfff);
_kernel_debug_buffer = _kernel_heap_start;
_end_kernel = .;
}
17 changes: 4 additions & 13 deletions setup32.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "setup32.h"
#include "apic.h"
#include "smp.h"
#include "debug.h"

// GDT data
struct gdt_entry __attribute__((aligned(8))) gdt[LAST_SEG/8];
Expand Down Expand Up @@ -77,17 +78,7 @@ void lapic_irq_handler_1();
__attribute__((regparm(0))) void trap_handler(struct regs_frame *rf) {

#ifdef DEBUG_TRAP
print_vga("Trap handler", true);

print_msg("gs", rf->gs, 16, false);
print_msg("fs", rf->fs, 16, false);
print_msg("es", rf->es, 16, false);
print_msg("ds", rf->ds, 16, false);
print_msg("code_nr", rf->code_nr, 16, false);
print_msg("cs", rf->cs, 16, false);
print_msg("eip", rf->eip, 16, false);
print_msg("ss", rf->ss, 16, false);
print_msg("esp", rf->esp, 16, false);
printf(KERNEL_DEBUG, "Trap handler\ngs: %x fs: %x es: %x ds: %x code_nr: %x cs: %x eip: %x ss: %x esp: %x\n", rf->gs, rf->fs, rf->es, rf->ds, rf->code_nr, rf->cs, rf->eip, rf->ss, rf->esp);
#else
UNUSED(rf);
#endif
Expand Down Expand Up @@ -133,7 +124,7 @@ __attribute__((regparm(0))) void common_interrupt_handler(struct regs_frame *rf)
__attribute__((regparm(0))) void common_sys_call_handler(struct regs_frame *rf) {

#ifdef DEBUG_SYSCALL
print_msg("System call", rf->code_nr | CUR_CPU, 16, true);
printf(KERNEL_DEBUG, "System call: %x\n", rf->code_nr | CUR_CPU, 16);
#else
UNUSED(rf);
#endif
Expand Down Expand Up @@ -299,5 +290,5 @@ void setup32() {

STI;

print_vga("Setup GDT,IDT, LDT and TSS done", true);
printf(KERNEL_INFO, "Setup GDT,IDT, LDT and TSS done\n");
}
7 changes: 4 additions & 3 deletions smp.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "setup32.h"
#include "usermode.h"
#include "page32.h"
#include "debug.h"

#define AP_INIT_PHYS_TEXT 0x7c00

Expand Down Expand Up @@ -71,7 +72,7 @@ void copy_smp_init_to_low_mem() {
char *s = (char*)&init_ap;
char *d = (char*)(AP_INIT_PHYS_TEXT+KERNEL_VIRT_ADDR);

print_msg("init_ap_size", (int)init_ap_size, 10, true);
printf(KERNEL_DEBUG, "init_ap_size: %d\n", (int)init_ap_size);

for(int i=0;i<(int)init_ap_size;i++) {
*d++ = *s++;
Expand Down Expand Up @@ -118,6 +119,6 @@ void smp_start() {
lapic_write_register(LAPIC_ICR_2, 0);
pit_wait_ms(200);

print_msg("Number of APs", smp_nums, 10, true);
print_msg("SMP bits", smp_bits, 16, true);
printf(KERNEL_INFO, "Number of APs: %d\n", smp_nums);
printf(KERNEL_INFO, "SMP bits: %x\n", smp_bits);
}
8 changes: 6 additions & 2 deletions start.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include "memory.h"
#include "usermode.h"
#include "smp.h"
#include "acpi.h"
#include "debug.h"

//
// Start kernel routine
Expand All @@ -25,11 +27,13 @@
int start_kernel(void) {

vga_init();
print_vga("XIS kernel started (v1.0)", true);
print_vga("", true);
debug_init();
printf(KERNEL_INFO, "XIS kernel started (v1.0)\n\n");
dump_e820();
setup32();
init_memory();
bda_read_table();
//acpi_find_rsdp();
usermode_load_first_program();
smp_start();
initialize_usermode();
Expand Down
Loading

0 comments on commit f834fb5

Please sign in to comment.