add full callback support

This commit is contained in:
Yanyan Jiang 2018-08-10 17:16:42 +00:00
parent b1f87c61fd
commit 00cabf5d98
3 changed files with 39 additions and 41 deletions

View File

@ -62,30 +62,28 @@ _Context *kcontext(_Area stack, void (*entry)(void *), void *arg) {
return ctx;
}
int yield() {
void yield() {
asm volatile("int $0x80" : : "a"(-1));
return 0;
}
int intr_read() {
return (get_efl() & FL_IF) != 0;
}
int intr_write(int enable) {
void intr_write(int enable) {
if (enable) {
sti();
} else {
cli();
}
return 0;
}
#define IRQ T_IRQ0 +
#define MSG(m) : ev.msg = m;
_Context *_irq_callback(_Event ev, _Context *ctx);
_Context *__cb_irq(_Event ev, _Context *ctx);
_Context *irq_callback(_Event ev, _Context *ctx) {
_Context *_cb_irq(_Event ev, _Context *ctx) {
return user_handler(ev, ctx);
}
@ -166,7 +164,7 @@ void irq_handle(struct TrapFrame *tf) {
// Call user handlers (registered in _asye_init)
_Context *ret_ctx = &ctx;
if (user_handler) {
_Context *next = _irq_callback(ev, &ctx);
_Context *next = __cb_irq(ev, &ctx);
if (!next) {
panic("return to a null context");
}
@ -205,4 +203,4 @@ iret:
"popl %ds;"
"iret;" // interrupt return
);
}
}

View File

@ -61,7 +61,7 @@ int protect(_Protect *p) {
return 0;
}
int unprotect(_Protect *p) {
void unprotect(_Protect *p) {
PDE *upt = p->ptr;
for (uint32_t va = (uint32_t)prot_vm_range.start;
va != (uint32_t)prot_vm_range.end;
@ -72,12 +72,10 @@ int unprotect(_Protect *p) {
}
}
pgfree(upt);
return 0;
}
int prot_switch(_Protect *p) {
void prot_switch(_Protect *p) {
set_cr3(p->ptr);
return 0;
}
int map(_Protect *p, void *va, void *pa, int prot) {
@ -118,8 +116,14 @@ _Context *ucontext(_Protect *p, _Area ustack, _Area kstack, void *entry, void *a
return ctx;
}
void *__cb_alloc(size_t size);
void __cb_free(void *ptr);
void *_cb_alloc(size_t size) { return pgalloc_usr(PGSIZE); }
void _cb_free(void *ptr) { pgfree_usr(ptr); }
static void *pgalloc() {
void *ret = pgalloc_usr(PGSIZE);
void *ret = __cb_alloc(PGSIZE);
if (!ret) panic("page allocation fail"); // for ease of debugging
for (int i = 0; i < PGSIZE / sizeof(uint32_t); i++) {
((uint32_t *)ret)[i] = 0;
@ -128,5 +132,5 @@ static void *pgalloc() {
}
static void pgfree(void *ptr) {
pgfree_usr(ptr);
__cb_free(ptr);
}

View File

@ -39,24 +39,18 @@ void _trace_off(uint32_t flags) {
.a2 = ((uintptr_t)_2), \
.a3 = ((uintptr_t)_3), } \
#define trace_wrapper_noret(rettype, stub, func, arglist, n, ...) \
#define trace_wrapper(rettype, stub, func, arglist, n, ...) \
trace_wrapper_noret(rettype, stub, func, arglist, n, __VA_ARGS__); \
return ret;
#define TRACE_NORET(rettype, stub, func, decl, arglist, n, ...) \
void stub decl { \
TRACE_CALL(stub, CALL_ARGS(__VA_ARGS__, 0, 0, 0, 0)); \
#define TRACE_VOID(rettype, func, decl, arglist, n, ...) \
void _##func decl { \
TRACE_CALL(func, CALL_ARGS(__VA_ARGS__, 0, 0, 0, 0)); \
func arglist; \
TRACE_RET(stub, 0); \
TRACE_RET(func, 0); \
}
#define TRACE(rettype, stub, func, decl, arglist, n, ...) \
rettype stub decl { \
TRACE_CALL(stub, CALL_ARGS(__VA_ARGS__, 0, 0, 0, 0)); \
#define TRACE_FUNC(rettype, func, decl, arglist, n, ...) \
rettype _##func decl { \
TRACE_CALL(func, CALL_ARGS(__VA_ARGS__, 0, 0, 0, 0)); \
rettype ret = func arglist; \
TRACE_RET(stub, ret); \
TRACE_RET(func, ret); \
return ret; \
}
@ -64,23 +58,25 @@ void _trace_off(uint32_t flags) {
rettype func decl ;
#define DEF(tr, rettype, func, decl, args, ...) \
tr(rettype, _##func, func, decl, args, __VA_ARGS__)
TRACE_##tr(rettype, func, decl, args, __VA_ARGS__)
#define ASYE_FUNCS(_) \
_(TRACE, int, asye_init, (_Context *(*handler)(_Event, _Context *)), (handler), 1, handler) \
_(TRACE, _Context *, kcontext, (_Area stack, void (*entry)(void *), void *arg), (stack, entry, arg), 2, entry, arg) \
_(TRACE, int, intr_read, (), (), 1, 0) \
_(TRACE, _Context *, irq_callback, (_Event ev, _Context *ctx), (ev, ctx), 4, ev.event, ev.cause, ev.ref, ctx); \
_(TRACE_NORET, int, yield, (), (), 1, 0) \
_(TRACE_NORET, int, intr_write, (int enable), (enable), 1, enable)
_(FUNC, int, asye_init, (_Context *(*handler)(_Event, _Context *)), (handler), 1, handler) \
_(FUNC, _Context *, kcontext, (_Area stack, void (*entry)(void *), void *arg), (stack, entry, arg), 2, entry, arg) \
_(VOID, void, yield, (), (), 1, 0) \
_(FUNC, int, intr_read, (), (), 1, 0) \
_(VOID, void, intr_write, (int enable), (enable), 1, enable) \
_(FUNC, _Context *, _cb_irq, (_Event ev, _Context *ctx), (ev, ctx), 4, ev.event, ev.cause, ev.ref, ctx); \
#define PTE_FUNCS(_) \
_(TRACE, int, pte_init, (void * (*pgalloc_f)(size_t), void (*pgfree_f)(void *)), (pgalloc_f, pgfree_f), 2, pgalloc_f, pgfree_f) \
_(TRACE, int, map, (_Protect *p, void *va, void *pa, int prot), (p, va, pa, prot), 4, p, va, pa, prot) \
_(TRACE, _Context *, ucontext, (_Protect *p, _Area ustack, _Area kstack, void *entry, void *args), (p, ustack, kstack, entry, args), 3, p, entry, args) \
_(TRACE_NORET, int, prot_switch, (_Protect *p), (p), 1, p) \
_(TRACE, int, protect, (_Protect *p), (p), 1, p) \
_(TRACE_NORET, int, unprotect, (_Protect *p), (p), 1, p)
_(FUNC, int, pte_init, (void * (*pgalloc_f)(size_t), void (*pgfree_f)(void *)), (pgalloc_f, pgfree_f), 2, pgalloc_f, pgfree_f) \
_(FUNC, int, protect, (_Protect *p), (p), 1, p) \
_(VOID, void, unprotect, (_Protect *p), (p), 1, p) \
_(VOID, void, prot_switch, (_Protect *p), (p), 1, p) \
_(FUNC, int, map, (_Protect *p, void *va, void *pa, int prot), (p, va, pa, prot), 4, p, va, pa, prot) \
_(FUNC, _Context *, ucontext, (_Protect *p, _Area ustack, _Area kstack, void *entry, void *args), (p, ustack, kstack, entry, args), 3, p, entry, args) \
_(FUNC, void *, _cb_alloc, (size_t size), (size), 1, size) \
_(VOID, void, _cb_free, (void *ptr), (ptr), 1, ptr) \
// ========== real definitions are generated below ==========