format refactors
This commit is contained in:
parent
fb27cbbce3
commit
bcd3e2853f
2
am/am.h
2
am/am.h
|
@ -36,7 +36,7 @@ enum {
|
||||||
// Memory area for [@start, @end)
|
// Memory area for [@start, @end)
|
||||||
typedef struct _Area {
|
typedef struct _Area {
|
||||||
void *start, *end;
|
void *start, *end;
|
||||||
} _Area;
|
} _Area;
|
||||||
|
|
||||||
// An event of type @event, caused by @cause of pointer @ref
|
// An event of type @event, caused by @cause of pointer @ref
|
||||||
typedef struct _Event {
|
typedef struct _Event {
|
||||||
|
|
|
@ -9,7 +9,7 @@ extern "C" {
|
||||||
|
|
||||||
// =========================== AM Devices ============================
|
// =========================== AM Devices ============================
|
||||||
|
|
||||||
#define _DEV_PERFCNT 0x0000ac01 // AM Virtual Performance Counter
|
#define _DEV_PERFCNT 0x0000ac01 // AM Virtual Performance Counter
|
||||||
#define _DEV_INPUT 0x0000ac02 // AM Virtual Input Device
|
#define _DEV_INPUT 0x0000ac02 // AM Virtual Input Device
|
||||||
#define _DEV_TIMER 0x0000ac03 // AM Virtual Timer
|
#define _DEV_TIMER 0x0000ac03 // AM Virtual Timer
|
||||||
#define _DEV_VIDEO 0x0000ac04 // AM Virtual Video Controller
|
#define _DEV_VIDEO 0x0000ac04 // AM Virtual Video Controller
|
||||||
|
|
|
@ -20,7 +20,7 @@ _Context* __am_irq_handle(_Context *c) {
|
||||||
case 0: ev.event = _EVENT_IRQ_TIMER; break;
|
case 0: ev.event = _EVENT_IRQ_TIMER; break;
|
||||||
case 2:
|
case 2:
|
||||||
case 3: __am_tlb_refill(); return next;
|
case 3: __am_tlb_refill(); return next;
|
||||||
case 8:
|
case 8:
|
||||||
syscall_instr = *(uint32_t *)(c->epc);
|
syscall_instr = *(uint32_t *)(c->epc);
|
||||||
ev.event = ((syscall_instr >> 6) == 1) ? _EVENT_YIELD : _EVENT_SYSCALL;
|
ev.event = ((syscall_instr >> 6) == 1) ? _EVENT_YIELD : _EVENT_SYSCALL;
|
||||||
c->epc += 4;
|
c->epc += 4;
|
||||||
|
|
|
@ -20,7 +20,7 @@ static int event_thread(void *args) {
|
||||||
SDL_WaitEvent(&event);
|
SDL_WaitEvent(&event);
|
||||||
switch (event.type) {
|
switch (event.type) {
|
||||||
case SDL_QUIT: _exit(0); break;
|
case SDL_QUIT: _exit(0); break;
|
||||||
case SDL_KEYDOWN:
|
case SDL_KEYDOWN:
|
||||||
case SDL_KEYUP:
|
case SDL_KEYUP:
|
||||||
{
|
{
|
||||||
SDL_Keysym k = event.key.keysym;
|
SDL_Keysym k = event.key.keysym;
|
||||||
|
|
|
@ -97,7 +97,7 @@ int _map(_AddressSpace *as, void *va, void *pa, int prot) {
|
||||||
else {
|
else {
|
||||||
pp->is_mapped = false;
|
pp->is_mapped = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,7 +22,7 @@ int __am_event_thread(void) {
|
||||||
if (event.type == NDL_EVENT_TIMER) {
|
if (event.type == NDL_EVENT_TIMER) {
|
||||||
__am_set_systime(event.data);
|
__am_set_systime(event.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event.type == NDL_EVENT_KEYUP || event.type == NDL_EVENT_KEYDOWN) {
|
if (event.type == NDL_EVENT_KEYUP || event.type == NDL_EVENT_KEYDOWN) {
|
||||||
int keydown = event.type == NDL_EVENT_KEYDOWN;
|
int keydown = event.type == NDL_EVENT_KEYDOWN;
|
||||||
int scancode = event.data;
|
int scancode = event.data;
|
||||||
|
|
|
@ -67,12 +67,12 @@ void load_kernel(void) {
|
||||||
struct elf32_hdr *elf32 = (void *)0x8000;
|
struct elf32_hdr *elf32 = (void *)0x8000;
|
||||||
struct elf64_hdr *elf64 = (void *)0x8000;
|
struct elf64_hdr *elf64 = (void *)0x8000;
|
||||||
int is_ap = boot_record()->is_ap;
|
int is_ap = boot_record()->is_ap;
|
||||||
|
|
||||||
if (!is_ap) {
|
if (!is_ap) {
|
||||||
// load argument (string) to memory
|
// load argument (string) to memory
|
||||||
copy_from_disk((void *)MAINARG_ADDR, 1024, -1024);
|
copy_from_disk((void *)MAINARG_ADDR, 1024, -1024);
|
||||||
// load elf header to memory
|
// load elf header to memory
|
||||||
copy_from_disk(elf32, 4096, 0);
|
copy_from_disk(elf32, 4096, 0);
|
||||||
if (elf32->e_machine == EM_X86_64) {
|
if (elf32->e_machine == EM_X86_64) {
|
||||||
load_elf64(elf64);
|
load_elf64(elf64);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -23,7 +23,7 @@ static void __am_irq_handle_internal(struct trap_frame *tf) {
|
||||||
.cause = 0, .ref = 0,
|
.cause = 0, .ref = 0,
|
||||||
.msg = "(no message)",
|
.msg = "(no message)",
|
||||||
};
|
};
|
||||||
|
|
||||||
#if __x86_64
|
#if __x86_64
|
||||||
saved_ctx = tf->saved_context;
|
saved_ctx = tf->saved_context;
|
||||||
saved_ctx.rip = tf->rip;
|
saved_ctx.rip = tf->rip;
|
||||||
|
@ -46,7 +46,7 @@ static void __am_irq_handle_internal(struct trap_frame *tf) {
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define IRQ T_IRQ0 +
|
#define IRQ T_IRQ0 +
|
||||||
#define MSG(m) ev.msg = m;
|
#define MSG(m) ev.msg = m;
|
||||||
|
|
||||||
if (IRQ 0 <= tf->irq && tf->irq < IRQ 32) {
|
if (IRQ 0 <= tf->irq && tf->irq < IRQ 32) {
|
||||||
|
@ -157,7 +157,7 @@ _Context *_kcontext(_Area stack, void (*entry)(void *), void *arg) {
|
||||||
ctx->rip = (uintptr_t)entry;
|
ctx->rip = (uintptr_t)entry;
|
||||||
ctx->rflags = FL_IF;
|
ctx->rflags = FL_IF;
|
||||||
ctx->rdi = (uintptr_t)arg;
|
ctx->rdi = (uintptr_t)arg;
|
||||||
void *stk[] = { panic_on_return };
|
void *stk[] = { panic_on_return };
|
||||||
#else
|
#else
|
||||||
#define sp esp
|
#define sp esp
|
||||||
ctx->esp = (uintptr_t)stack.end;
|
ctx->esp = (uintptr_t)stack.end;
|
||||||
|
@ -165,7 +165,7 @@ _Context *_kcontext(_Area stack, void (*entry)(void *), void *arg) {
|
||||||
ctx->cs = KSEL(SEG_KCODE);
|
ctx->cs = KSEL(SEG_KCODE);
|
||||||
ctx->eip = (uint32_t)entry;
|
ctx->eip = (uint32_t)entry;
|
||||||
ctx->eflags = FL_IF;
|
ctx->eflags = FL_IF;
|
||||||
void *stk[] = { panic_on_return, arg };
|
void *stk[] = { panic_on_return, arg };
|
||||||
#endif
|
#endif
|
||||||
ctx->sp -= sizeof(stk);
|
ctx->sp -= sizeof(stk);
|
||||||
for (int i = 0; i < LENGTH(stk); i++) {
|
for (int i = 0; i < LENGTH(stk); i++) {
|
||||||
|
|
|
@ -21,7 +21,7 @@ static int scan_code[] = {
|
||||||
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 43,
|
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 43,
|
||||||
58, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 28,
|
58, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 28,
|
||||||
42, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54,
|
42, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54,
|
||||||
29, 91, 56, 57, 56, 29,
|
29, 91, 56, 57, 56, 29,
|
||||||
72, 80, 75, 77
|
72, 80, 75, 77
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -328,7 +328,7 @@ void __am_percpu_initlapic(void) {
|
||||||
lapicw(SVR, ENABLE | (T_IRQ0 + IRQ_SPURIOUS));
|
lapicw(SVR, ENABLE | (T_IRQ0 + IRQ_SPURIOUS));
|
||||||
lapicw(TDCR, X1);
|
lapicw(TDCR, X1);
|
||||||
lapicw(TIMER, PERIODIC | (T_IRQ0 + IRQ_TIMER));
|
lapicw(TIMER, PERIODIC | (T_IRQ0 + IRQ_TIMER));
|
||||||
lapicw(TICR, 10000000);
|
lapicw(TICR, 10000000);
|
||||||
lapicw(LINT0, MASKED);
|
lapicw(LINT0, MASKED);
|
||||||
lapicw(LINT1, MASKED);
|
lapicw(LINT1, MASKED);
|
||||||
if (((__am_lapic[VER]>>16) & 0xFF) >= 4)
|
if (((__am_lapic[VER]>>16) & 0xFF) >= 4)
|
||||||
|
@ -360,8 +360,8 @@ void __am_lapic_bootap(uint32_t apicid, void *addr) {
|
||||||
lapicw(ICRHI, apicid<<24);
|
lapicw(ICRHI, apicid<<24);
|
||||||
lapicw(ICRLO, INIT | LEVEL | ASSERT);
|
lapicw(ICRLO, INIT | LEVEL | ASSERT);
|
||||||
lapicw(ICRLO, INIT | LEVEL);
|
lapicw(ICRLO, INIT | LEVEL);
|
||||||
|
|
||||||
for (i = 0; i < 2; i++){
|
for (i = 0; i < 2; i++){
|
||||||
lapicw(ICRHI, apicid<<24);
|
lapicw(ICRHI, apicid<<24);
|
||||||
lapicw(ICRLO, STARTUP | ((uintptr_t)addr>>12));
|
lapicw(ICRLO, STARTUP | ((uintptr_t)addr>>12));
|
||||||
}
|
}
|
||||||
|
|
|
@ -58,7 +58,6 @@ void _halt(int code) {
|
||||||
}
|
}
|
||||||
outw(0x604, 0x2000); // offer of qemu :)
|
outw(0x604, 0x2000); // offer of qemu :)
|
||||||
while (1) hlt();
|
while (1) hlt();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_Area __am_heap_init() {
|
_Area __am_heap_init() {
|
||||||
|
|
|
@ -15,7 +15,6 @@
|
||||||
#ifndef __ASSEMBLER__
|
#ifndef __ASSEMBLER__
|
||||||
|
|
||||||
#include <am.h>
|
#include <am.h>
|
||||||
#include <klib.h>
|
|
||||||
|
|
||||||
#define ROUNDUP(a, sz) ((((uintptr_t)a)+(sz)-1) & ~((sz)-1))
|
#define ROUNDUP(a, sz) ((((uintptr_t)a)+(sz)-1) & ~((sz)-1))
|
||||||
#define ROUNDDOWN(a, sz) ((((uintptr_t)a)) & ~((sz)-1))
|
#define ROUNDDOWN(a, sz) ((((uintptr_t)a)) & ~((sz)-1))
|
||||||
|
|
|
@ -1,99 +1,99 @@
|
||||||
/*
|
/*
|
||||||
* Static runtime library for a system software on AbstractMachine
|
* Static runtime library for a system software on AbstractMachine
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef __KLIB_H__
|
#ifndef __KLIB_H__
|
||||||
#define __KLIB_H__
|
#define __KLIB_H__
|
||||||
|
|
||||||
#include <am.h>
|
#include <am.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
|
||||||
//#define __NATIVE_USE_KLIB__
|
//#define __NATIVE_USE_KLIB__
|
||||||
|
|
||||||
#ifdef __NATIVE_USE_KLIB__
|
#ifdef __NATIVE_USE_KLIB__
|
||||||
#define strlen my_strlen
|
#define strlen my_strlen
|
||||||
#define strcpy my_strcpy
|
#define strcpy my_strcpy
|
||||||
#define strncpy my_strncpy
|
#define strncpy my_strncpy
|
||||||
#define strcat my_strcat
|
#define strcat my_strcat
|
||||||
#define strcmp my_strcmp
|
#define strcmp my_strcmp
|
||||||
#define strncmp my_strncmp
|
#define strncmp my_strncmp
|
||||||
#define memset my_memset
|
#define memset my_memset
|
||||||
#define memcpy my_memcpy
|
#define memcpy my_memcpy
|
||||||
#define memcmp my_memcmp
|
#define memcmp my_memcmp
|
||||||
#define printf my_printf
|
#define printf my_printf
|
||||||
#define vsprintf my_vsprintf
|
#define vsprintf my_vsprintf
|
||||||
#define sprintf my_sprintf
|
#define sprintf my_sprintf
|
||||||
#define snprintf my_snprintf
|
#define snprintf my_snprintf
|
||||||
#define malloc my_malloc
|
#define malloc my_malloc
|
||||||
#define free my_free
|
#define free my_free
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// am devices
|
// am devices
|
||||||
|
|
||||||
uint32_t uptime();
|
uint32_t uptime();
|
||||||
void get_timeofday(void *rtc);
|
void get_timeofday(void *rtc);
|
||||||
int read_key();
|
int read_key();
|
||||||
void draw_rect(uint32_t *pixels, int x, int y, int w, int h);
|
void draw_rect(uint32_t *pixels, int x, int y, int w, int h);
|
||||||
void draw_sync();
|
void draw_sync();
|
||||||
int screen_width();
|
int screen_width();
|
||||||
int screen_height();
|
int screen_height();
|
||||||
|
|
||||||
// string.h
|
// string.h
|
||||||
void* memset(void* v, int c, size_t n);
|
void *memset(void* v, int c, size_t n);
|
||||||
void* memcpy(void* dst, const void* src, size_t n);
|
void *memcpy(void* dst, const void* src, size_t n);
|
||||||
void* memmove(void* dst, const void* src, size_t n);
|
void *memmove(void* dst, const void* src, size_t n);
|
||||||
int memcmp(const void* s1, const void* s2, size_t n);
|
int memcmp(const void* s1, const void* s2, size_t n);
|
||||||
size_t strlen(const char* s);
|
size_t strlen(const char* s);
|
||||||
char* strcat(char* dst, const char* src);
|
char *strcat(char* dst, const char* src);
|
||||||
char* strcpy(char* dst, const char* src);
|
char *strcpy(char* dst, const char* src);
|
||||||
char* strncpy(char* dst, const char* src, size_t n);
|
char *strncpy(char* dst, const char* src, size_t n);
|
||||||
int strcmp(const char* s1, const char* s2);
|
int strcmp(const char* s1, const char* s2);
|
||||||
int strncmp(const char* s1, const char* s2, size_t n);
|
int strncmp(const char* s1, const char* s2, size_t n);
|
||||||
char* strtok(char* s,const char* delim);
|
char *strtok(char* s,const char* delim);
|
||||||
char *strstr(const char *, const char *);
|
char *strstr(const char *, const char *);
|
||||||
char *strchr(const char *s, int c);
|
char *strchr(const char *s, int c);
|
||||||
char *strrchr(const char *s, int c);
|
char *strrchr(const char *s, int c);
|
||||||
|
|
||||||
// stdlib.h
|
// stdlib.h
|
||||||
int atoi(const char* nptr);
|
int atoi(const char* nptr);
|
||||||
int abs(int x);
|
int abs(int x);
|
||||||
unsigned long time();
|
unsigned long time();
|
||||||
void srand(unsigned int seed);
|
void srand(unsigned int seed);
|
||||||
int rand();
|
int rand();
|
||||||
void *malloc(size_t size);
|
void *malloc(size_t size);
|
||||||
void free(void *ptr);
|
void free(void *ptr);
|
||||||
|
|
||||||
// stdio.h
|
// stdio.h
|
||||||
int printf(const char* fmt, ...);
|
int printf(const char* fmt, ...);
|
||||||
int sprintf(char* out, const char* format, ...);
|
int sprintf(char* out, const char* format, ...);
|
||||||
int snprintf(char* s, size_t n, const char* format, ...);
|
int snprintf(char* s, size_t n, const char* format, ...);
|
||||||
int vsprintf(char *str, const char *format, va_list ap);
|
int vsprintf(char *str, const char *format, va_list ap);
|
||||||
int vsnprintf(char *str, size_t size, const char *format, va_list ap);
|
int vsnprintf(char *str, size_t size, const char *format, va_list ap);
|
||||||
int sscanf(const char *str, const char *format, ...);
|
int sscanf(const char *str, const char *format, ...);
|
||||||
|
|
||||||
void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));
|
void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));
|
||||||
|
|
||||||
#define printk printf
|
#define printk printf
|
||||||
|
|
||||||
// assert.h
|
// assert.h
|
||||||
#ifdef NDEBUG
|
#ifdef NDEBUG
|
||||||
#define assert(ignore) ((void)0)
|
#define assert(ignore) ((void)0)
|
||||||
#else
|
#else
|
||||||
#define assert(cond) \
|
#define assert(cond) \
|
||||||
do { \
|
do { \
|
||||||
if (!(cond)) { \
|
if (!(cond)) { \
|
||||||
printk("Assertion fail at %s:%d\n", __FILE__, __LINE__); \
|
printk("Assertion fail at %s:%d\n", __FILE__, __LINE__); \
|
||||||
_halt(1); \
|
_halt(1); \
|
||||||
} \
|
} \
|
||||||
} while (0)
|
} while (0)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue