4 Commits

Author SHA1 Message Date
3c03c9d383 Fix: uart_hex wrong offset 2025-04-15 17:44:28 +08:00
c4a44542ef Fix: interrupt preemption 2025-04-15 17:22:45 +08:00
89ff8a018d Fix: page state 2025-04-15 15:12:07 +08:00
f25758031a Fix: mman behavior 2025-04-15 14:20:52 +08:00
14 changed files with 192 additions and 86 deletions

View File

@@ -101,11 +101,11 @@ logger_cur = _Generic((msg), \
// #define DEBUG_DTB(val) DEBUG(val)
#define DEBUG_DTB(val) CLEAN
#define DEBUG_EXCEP(val) DEBUG(val)
// #define DEBUG_EXCEP(val) CLEAN
// #define DEBUG_EXCEP(val) DEBUG(val)
#define DEBUG_EXCEP(val) CLEAN
// #define DEBUG_MEM(val) DEBUG(val)
#define DEBUG_MEM(val) CLEAN
#define DEBUG_MEM(val) DEBUG(val)
// #define DEBUG_MEM(val) CLEAN
// #define DEBUG_INITRD(val) DEBUG(val)
#define DEBUG_INITRD(val) CLEAN

View File

@@ -11,6 +11,8 @@ typedef struct interrupt {
uint64_t priority;
interrupt_callback_func_t func;
uint64_t param;
int is_start;
} interrupt_t;
void add_interrupt_task(uint64_t priority,

View File

@@ -19,7 +19,11 @@ typedef enum :uint8_t {
typedef struct {
page_state_t state;
uint64_t maxsz;
// when state == PAGE_ALLOCATED or PAGE_RESERVED, size = 0
// when state == PAGE_FREE, size = r - l
// when state == PAGE_DIVIDED, size = LCH->size | RCH->size
uint64_t size;
} page_header_t;
void mman_init();

View File

@@ -20,7 +20,7 @@ typedef struct timer {
task_t data;
} timer_t;
void add_task(task_t task);
void add_timer_task(task_t task);
void timer_irq_handler(void);

View File

@@ -6,6 +6,8 @@
uint32_t msb32(uint32_t);
uint64_t msb64(uint64_t);
uint64_t lsb64(uint64_t);
uint32_t hton32(const uint32_t);
uint32_t ntoh32(const uint32_t);

View File

@@ -21,7 +21,7 @@ void not_implemented_handler()
void synchronous_handler()
{
static int poop = 0;
if (poop++ > 5)
if (poop++ > 10)
exit(ERR_ADMINKILL);
DEBUG_EXCEP("synchronous caught");
uint64_t x0 = 0x0;

View File

@@ -73,21 +73,27 @@ void add_interrupt_task(uint64_t priority,
.priority = priority,
.func = func,
.param = param
.param = param,
.is_start = false,
};
global_interrupt_pool = _merge(global_interrupt_pool, newint);
while (global_interrupt_pool) {
// LOG("check interrupt"); DEBUG_EXCEP(global_interrupt_pool->priority);
if (global_interrupt_pool->is_start)
return;
global_interrupt_pool->is_start = true;
global_interrupt_pool->func(global_interrupt_pool->param);
global_interrupt_pool = _pop(global_interrupt_pool);
}
}
void init_interrupt(void)
{
uint64_t cntfrq_el0;
W_SYSREG(cntp_ctl_el0, 1);
R_SYSREG(cntfrq_el0, cntfrq_el0);
W_SYSREG(cntp_tval_el0, cntfrq_el0);
_enable_timer_irq(true);
_enable_interrupt();
_enable_timer_irq(true);
}
void irq_handler(void)
@@ -104,6 +110,8 @@ void irq_handler(void)
void wfe(void)
{
return; // do nothing for now
if (!global_interrupt_pool) {
// asm volatile("wfe");
asm volatile("nop");

View File

@@ -1,4 +1,4 @@
#include <uart.h>
#include <logger.h>
#include <errcode.h>
#include <utils.h>
#include <kmalloc.h>

View File

@@ -47,23 +47,24 @@ void _pull(int idx, size_t sz)
{
if (LCH->state == PAGE_FREE && RCH->state == PAGE_FREE) {
if (CUR->state != PAGE_FREE) {
LOG("node merged");
LOG("free page merged");
DEBUG_MEM((uint64_t)idx);
}
CUR->state = PAGE_FREE;
CUR->size = sz;
}
if (LCH->state != PAGE_FREE || RCH->state != PAGE_FREE)
CUR->state = PAGE_DIVIDED;
switch (CUR->state) {
case PAGE_FREE:
CUR->maxsz = sz;
CUR->size = sz;
break;
case PAGE_DIVIDED:
CUR->maxsz = MAX(LCH->maxsz, RCH->maxsz);
CUR->size = LCH->size | RCH->size;
break;
case PAGE_ALLOCATED:
case PAGE_RESERVED:
CUR->maxsz = 0;
CUR->size = 0;
break;
default:
exit(ERR_UNREACHABLE);
@@ -85,7 +86,7 @@ void mman_init()
mman_frame_array[0] = (page_header_t){
.state = PAGE_FREE,
.maxsz = msb64(mman_page_cnt),
.size = mman_page_cnt,
};
fdt_reserve_entry_t *entry = 0x0;
@@ -109,7 +110,7 @@ static inline
uint64_t _allocate_page(size_t req, int idx, uint64_t l, uint64_t r)
{
uint64_t sz = r - l;
if (req > sz || req > CUR->maxsz) {
if (req > sz || req > CUR->size) {
return MMAN_NO_PAGE;
}
@@ -117,16 +118,15 @@ uint64_t _allocate_page(size_t req, int idx, uint64_t l, uint64_t r)
switch (CUR->state) {
case PAGE_FREE:
if (req == sz) {
LOG("page allocated");
LOG(l);
DEBUG_MEM(r);
LOG("page allocated"); LOG(l); DEBUG_MEM(r);
CUR->state = PAGE_ALLOCATED;
CUR->maxsz = 0;
CUR->size = 0;
return l;
}
LOG("page divided"); LOG(l); DEBUG_MEM(r);
LCH->state = RCH->state = PAGE_FREE;
LCH->maxsz = msb64(m - l);
RCH->maxsz = msb64(r - m);
LCH->size = m - l;
RCH->size = r - m;
break;
case PAGE_DIVIDED:
break;
@@ -138,10 +138,25 @@ uint64_t _allocate_page(size_t req, int idx, uint64_t l, uint64_t r)
}
uint64_t ret = MMAN_NO_PAGE;
if (ret == MMAN_NO_PAGE && LCH->maxsz >= req)
// Goto child where size >= req
// If both children can handle, choose the one that has the least significant
// bit that can handle.
// It both children are the same, choose the left one
int is_lch_valid = (LCH->size >= req);
int is_rch_valid = (RCH->size >= req);
if (is_lch_valid && is_rch_valid) {
if (lsb64(LCH->size & ~(req - 1)) <= lsb64(RCH->size & ~(req - 1)))
ret = _allocate_page(req, LIDX, l, m);
else
ret = _allocate_page(req, RIDX, m, r);
}
if (ret == MMAN_NO_PAGE && LCH->size >= req)
ret = _allocate_page(req, LIDX, l, m);
if (ret == MMAN_NO_PAGE && RCH->maxsz >= req)
if (ret == MMAN_NO_PAGE && RCH->size >= req)
ret = _allocate_page(req, RIDX, m, r);
_pull(idx, sz);
return ret;
}
@@ -174,7 +189,7 @@ void _free_page(uint64_t req, int idx, uint64_t l, uint64_t r)
LOG(l);
DEBUG_MEM(r);
CUR->state = PAGE_FREE;
CUR->maxsz = sz;
CUR->size = sz;
return;
}
case PAGE_DIVIDED:
@@ -218,15 +233,17 @@ void _reserve_page(uint64_t ql, uint64_t qr, int idx, uint64_t l, uint64_t r)
LOG(l);
DEBUG_MEM(r);
CUR->state = PAGE_RESERVED;
CUR->maxsz = 0;
CUR->size = 0;
return;
}
uint64_t m = l + ((msb64(sz) == sz) ? (sz >> 1) : msb64(sz));
if (CUR->state == PAGE_FREE) {
LOG("page divided"); LOG(l); DEBUG_MEM(r);
CUR->state = PAGE_DIVIDED;
LCH->state = RCH->state = PAGE_FREE;
LCH->maxsz = msb64(m - l);
RCH->maxsz = msb64(r - m);
LCH->size = m - l;
RCH->size = r - m;
}
if (ql < m)

View File

@@ -30,6 +30,7 @@ void _help (void)
"cat <file> : concatenate files and print" ENDL
"exec <file> : execute file in usermode" ENDL
"settimeout <sec> <message...>: pring message after a few seconds" ENDL
"time : print current time tick" ENDL
"reboot : reboot the device" ENDL
);
}
@@ -74,6 +75,14 @@ void _memalloc(size_t size)
uart_puts(ENDL);
}
static inline
void _free(uint64_t ptr)
{
kfree((void *)ptr);
LOG(ptr);
INFOR("has been freed");
}
static inline
void _ls_initrd_callback(file_node_t *tr)
{
@@ -116,6 +125,7 @@ static inline
void _settimeout_cb_func(uint64_t args)
{
vector_t *v = (void *)args;
uart_puts("timeout: ");
for (int i = 2; i < (int)v->size; ++i) {
uart_puts(VEC_AT(char, v, i));
uart_puts(" ");
@@ -138,7 +148,22 @@ void _settimeout(int32_t sec, vector_t *args)
.func = _settimeout_cb_func,
.param = (uint64_t)args,
};
add_task(task);
add_timer_task(task);
}
static inline
void _time()
{
uint64_t cntpct_el0, cntfrq_el0, cntp_cval_el0, cntp_tval_el0;
R_SYSREG(cntpct_el0, cntpct_el0);
R_SYSREG(cntfrq_el0, cntfrq_el0);
R_SYSREG(cntp_cval_el0, cntp_cval_el0);
R_SYSREG(cntp_tval_el0, cntp_tval_el0);
LOG(cntpct_el0);
LOG(cntfrq_el0);
LOG(cntp_cval_el0);
INFOR(cntp_tval_el0);
}
static inline
@@ -209,9 +234,12 @@ int shell(file_node_t *initrd_root)
_hello();
} else if (!strcmp(cmd, "hwinfo")) {
_hwinfo();
} else if (!strcmp(cmd, "memalloc") && args->size >= 2){
} else if (!strcmp(cmd, "memalloc") && args->size >= 2) {
char *p1 = VEC_AT(char, args, 1);
_memalloc((size_t)atoi32(p1));
} else if (!strcmp(cmd, "free") && args->size >= 2) {
char *p1 = VEC_AT(char, args, 1);
_free((uint64_t)atoh32(p1));
} else if (!strcmp(cmd, "ls")) {
_ls(initrd_root);
} else if (!strcmp(cmd, "cat") && args->size >= 2) {
@@ -223,6 +251,8 @@ int shell(file_node_t *initrd_root)
} else if (!strcmp(cmd, "settimeout") && args->size >= 2) {
char *p1 = VEC_AT(char, args, 1);
_settimeout(atoi32(p1), args);
} else if (!strcmp(cmd, "time")) {
_time();
} else if (!strcmp(cmd, "reboot")) {
_reboot();
}

View File

@@ -47,18 +47,21 @@ timer_t *_pop(timer_t *t)
}
static inline
void _set_timer_interrrupt()
void _check_enable_timer()
{
if (global_timer) {
uint64_t cntpct_el0;
R_SYSREG(cntpct_el0, cntpct_el0);
DEBUG_EXCEP(cntpct_el0);
uint64_t firing_tick = MAX(global_timer->data.firing_tick, cntpct_el0);
W_SYSREG(cntp_cval_el0, firing_tick);
if (global_timer->data.firing_tick < cntpct_el0)
W_SYSREG(cntp_tval_el0, 1);
else
W_SYSREG(cntp_cval_el0, global_timer->data.firing_tick);
_enable_timer_irq(true);
}
W_SYSREG(cntp_ctl_el0, 1);
} else
W_SYSREG(cntp_ctl_el0, 0);
}
static inline
@@ -71,9 +74,9 @@ void _traverse(timer_t *t)
_traverse(t->_r);
}
void add_task(task_t task)
void add_timer_task(task_t task)
{
DEBUG_EXCEP("add task");
DEBUG_EXCEP("add timer task");
timer_t *newtimer = kmalloc(sizeof(timer_t));
*newtimer = (timer_t){
._l = (timer_t *)0x0,
@@ -85,24 +88,43 @@ void add_task(task_t task)
global_timer = _merge(global_timer, newtimer);
_traverse(global_timer);
_set_timer_interrrupt();
// _traverse(global_timer);
_check_enable_timer();
}
typedef struct {
interrupt_callback_func_t func;
uint64_t param;
} _timer_task_wrapper_param_t;
static inline
void _timer_task_wrapper(uint64_t param)
{
_timer_task_wrapper_param_t *data = (void *)param;
data->func(data->param);
kfree(data);
_enable_timer_irq(true);
}
void timer_irq_handler(void)
{
_enable_timer_irq(false);
LOG("timer irq");
uint64_t cntpct_el0;
R_SYSREG(cntpct_el0, cntpct_el0);
DEBUG_EXCEP(cntpct_el0);
LOG("timer irq"); DEBUG_EXCEP(cntpct_el0);
if (global_timer) {
add_interrupt_task(20, global_timer->data.func, global_timer->data.param);
global_timer = _pop(global_timer);
}
_timer_task_wrapper_param_t *param =
kmalloc(sizeof(_timer_task_wrapper_param_t));
*param = (_timer_task_wrapper_param_t){
.func = global_timer->data.func,
.param = global_timer->data.param,
};
add_interrupt_task(20, _timer_task_wrapper, (uint64_t)param);
_set_timer_interrrupt();
global_timer = _pop(global_timer);
_check_enable_timer();
}
}

View File

@@ -29,6 +29,8 @@ size_t (*uart_putb)(const uint8_t *bytes, size_t len) = uart_putb_sync;
size_t (*uart_getb)(uint8_t *bytes, size_t len) = uart_getb_sync;
int is_uart_inited = false;
int is_uart_in_interrupt_queue = false;
ringbuffer_t *uart_readbuf;
ringbuffer_t *uart_writebuf;
@@ -83,37 +85,47 @@ void uart_init(void)
is_uart_inited = true;
}
void uart_transmit_interrupt_handler()
static inline
void _enable_uart_interrupt()
{
_uart_enable_receive_interrupt(true);
if (uart_writebuf->size > 0)
_uart_write_data(ringbuffer_bump(uart_writebuf));
_uart_enable_transmit_interrupt(true);
}
typedef struct {
ringbuffer_t *buf;
uint8_t val;
} uart_interrupt_callback_payload_t;
static inline
void uart_receive_interrupt_callback(uint64_t param)
void _uart_transmit_interrupt_callback(uint64_t)
{
uart_interrupt_callback_payload_t *payload = (void *)param;
ringbuffer_push(payload->buf, payload->val);
kfree(payload);
_uart_write_data(ringbuffer_bump(uart_writebuf));
is_uart_in_interrupt_queue = false;
_enable_uart_interrupt();
}
void uart_receive_interrupt_handler()
static inline
void _uart_transmit_interrupt_handler()
{
if (uart_writebuf->size > 0) {
is_uart_in_interrupt_queue = true;
add_interrupt_task(11, _uart_transmit_interrupt_callback, 0x0);
}
}
static inline
void _uart_receive_interrupt_callback(uint64_t)
{
ringbuffer_push(uart_readbuf, _uart_read_data());
is_uart_in_interrupt_queue = false;
_enable_uart_interrupt();
}
static inline
void _uart_receive_interrupt_handler()
{
if (uart_readbuf->size < uart_readbuf->cap) {
uint8_t b = _uart_read_data();
uart_interrupt_callback_payload_t *param = kmalloc(
sizeof(uart_interrupt_callback_payload_t));
*param = (uart_interrupt_callback_payload_t){
.buf = uart_readbuf,
.val = b,
};
add_interrupt_task(10, uart_receive_interrupt_callback, (uint64_t)param);
is_uart_in_interrupt_queue = true;
add_interrupt_task(12, _uart_receive_interrupt_callback, 0x0);
}
}
@@ -121,14 +133,11 @@ void uart_irq_handler(void)
{
_uart_enable_receive_interrupt(false);
_uart_enable_transmit_interrupt(false);
if (_uart_receive_interrupt())
uart_receive_interrupt_handler();
_uart_receive_interrupt_handler();
if (_uart_transmit_interrupt())
uart_transmit_interrupt_handler();
_uart_enable_receive_interrupt(true);
if (uart_writebuf->size > 0)
_uart_enable_transmit_interrupt(true);
_uart_transmit_interrupt_handler();
}
size_t uart_putb_async(const uint8_t *bytes, size_t len)
@@ -137,8 +146,8 @@ size_t uart_putb_async(const uint8_t *bytes, size_t len)
for (; sentlen < len; ++bytes, ++sentlen)
ringbuffer_push(uart_writebuf, *bytes);
if (uart_writebuf->size > 0)
_uart_enable_transmit_interrupt(true);
if (!is_uart_in_interrupt_queue && uart_writebuf)
_enable_uart_interrupt();
return sentlen;
}
@@ -200,11 +209,10 @@ void uart_puts(const char *s)
void uart_hex(uint64_t d)
{
char buf[0x11], *p = buf;
uint64_t n;
for (int c = 28; c >= 0; c -= 4) {
for (int c = 28, n; c >= 0; c -= 4) {
n = (d >> c) & 0xf;
n += n > 9 ? (char)'A' : (char)'0';
*p++ = n;
n += (n > 9) ? 0x37 : 0x30;
*p++ = (char)n;
}
*p = '\0';
uart_puts(buf);

View File

@@ -24,6 +24,19 @@ uint64_t msb64(uint64_t x)
return res == 64 ? 0 : ((uint64_t)1 << (63 - res));
}
uint64_t lsb64(uint64_t x)
{
uint64_t res = 0x0;
asm volatile(
"rbit x0, %[val]" ENDL
"clz %[res], x0"
: [res] "=r" (res)
: [val] "r" (x)
: "x0"
);
return res == 64 ? 0 : ((uint64_t)1 << res);
}
uint32_t hton32(const uint32_t h)
{
const uint8_t *p = (const void *)&h;
@@ -106,7 +119,7 @@ uint32_t atoh32(const char *s)
exit(ERR_CONVERSION);
uint32_t ret = 0;
for (int i = 8; i--; ++s) {
for (int i = 8; i-- && *s != '\0'; ++s) {
if (!isxdigit(*s))
exit(ERR_CONVERSION);
ret <<= 4, ret |= (isdigit(*s) ? *s - '0' :

View File

@@ -54,7 +54,7 @@ void _print_time(uint64_t)
.param = 0x0,
};
add_task(task);
add_timer_task(task);
}
void main(void *dtb)