Skip to content

Instantly share code, notes, and snippets.

@benmezger
Created October 5, 2019 21:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benmezger/b06d99c29a9205c6f3403c887e3c5767 to your computer and use it in GitHub Desktop.
Save benmezger/b06d99c29a9205c6f3403c887e3c5767 to your computer and use it in GitHub Desktop.
void
switch_to_process(process_t *proc)
{
/* Set the entry point in MEPC */
asm volatile("csrw mepc, %0" ::"r"(proc->entrypoint));
asm volatile("mv ra, %0" ::"r"(proc->regf->ra));
asm volatile("mv sp, %0" ::"r"(proc->regf->sp));
asm volatile("mv a0, %0" ::"r"(proc));
asm volatile("mret");
}
.....
if (!(kcontext->user_context = malloc(sizeof(uctxt_t *)))) {
log_fatal("Error allocating memory for user context");
}
kcontext->user_context->regf = create_regf(1000);
kcontext->user_context->total_procs = 0;
_init_timer_intrp(kcontext);
log_info("Droping to user-mode");
drop_to_mode(
PRIVILEGE_USER, kcontext->user_context, user_mode_entrypoint);
}
void
kexcep_timer_handler(int id, void *data)
{
kctxt_t *kcontext = (kctxt_t *)data;
disable_timer(kcontext->timer_intrid);
sched(kcontext, kcontext->user_context->total_procs);
metal_cpu_set_mtimecmp(
kcontext->cpu, metal_cpu_get_mtime(kcontext->cpu) + RTC_FREQ);
log_warn(
"[kexcep_timer_handler] | Exception | Timer interrupt %p | id %d | mtime %ld | timebase %ld",
kcontext->timer_intr, id, metal_cpu_get_mtime(kcontext->cpu),
metal_cpu_get_timebase(kcontext->cpu));
enable_timer(kcontext->timer_intrid);
// return_from_excp(kctxt->cpu, id);
}
void
process_a(void *data)
{
for (;;) {
printf("Hello, from Process A");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment