Guest User

Untitled

a guest
Apr 30th, 2025
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.39 KB | Source Code | 0 0
  1. #include <kstdio.h>
  2. #include <kstring.h>
  3. #include <kstdlib.h>
  4. #include <sched.h>
  5. #include <mm/mm.h>
  6. #include <spinlock.h>
  7. #include <devices/timer.h>
  8.  
  9.  
  10.  
  11.  
  12.  
  13. static spinlock_t s;
  14. static task_t *task_list = NULL;
  15. static int next_task_id = 1;
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25. void sched_init(void) {
  26.     spinlock_init(&s);
  27. }
  28.  
  29. void sched_create_task(void (*__start)()) {
  30.     task_t *new_task = NULL;
  31.  
  32.  
  33.     spinlock_acquire(&s);
  34.  
  35.     new_task = kmalloc(sizeof(task_t));
  36.  
  37.     if (!new_task) {
  38.         printf("SCHED: Failed to create new task! (out of memory)\n");
  39.  
  40.         spinlock_release(&s);
  41.         return;
  42.     }
  43.  
  44.     memset(new_task, 0, sizeof(task_t));
  45.  
  46.     memset(&new_task->ctx, 0, sizeof(task_context_t));
  47.  
  48.     new_task->task_id = next_task_id++;
  49.     new_task->next = NULL;
  50.  
  51.     new_task->stack_size = TASK_STACK_SIZE;
  52.     new_task->stack = kmalloc(new_task->stack_size);
  53.  
  54.     if (!new_task->stack) {
  55.         printf("SCHED: Failed to allocate new task stack! (out of memory)\n");
  56.        
  57.         kfree(new_task, sizeof(task_t));
  58.         spinlock_release(&s);
  59.         return;
  60.     }
  61.  
  62.     memset(new_task->stack, 0, new_task->stack_size);
  63.  
  64.     uintptr_t sp = (uintptr_t)new_task->stack + new_task->stack_size;
  65.     sp = ALIGN_DOWN(sp, 16);
  66.  
  67.     new_task->ctx.sp = sp;
  68.     new_task->ctx.elr_el1 = (uint64_t)(uintptr_t)__start;
  69.     new_task->ctx.spsr_el1 = __spsr_read();
  70.  
  71.     new_task->task_state |= TASK_STATE_READY;
  72.  
  73.     if (!task_list) {
  74.         task_list = new_task;
  75.         task_list->next = new_task;
  76.     } else {
  77.         task_t *curr = task_list;
  78.  
  79.         while(curr->next != task_list) {
  80.             curr = curr->next;
  81.         }
  82.  
  83.         curr->next = new_task;
  84.         new_task->next = task_list;
  85.     }
  86.  
  87.     printf("SCHED: Created new task ID: %d, SP: 0x%lx, ELR: 0x%lx, SPSR: 0x%lx\n", new_task->task_id, new_task->ctx.sp, new_task->ctx.elr_el1, new_task->ctx.spsr_el1);
  88.     spinlock_release(&s);
  89. }
  90.  
  91. task_t *sched_get_current_task(void) {
  92.     if (!task_list) {
  93.         return NULL;
  94.     }
  95.  
  96.     return task_list;
  97. }
  98.  
  99. void sched_context_switch(task_context_t *old, task_context_t *new) {
  100.     if (!old || !new) {
  101.         return;
  102.     }
  103.  
  104.     __context_switch(old, new);
  105. }
  106.  
  107. void sched_context_restore(task_context_t *ctx) {
  108.     __context_restore(ctx);
  109. }
  110.  
  111. void sched_yield(void) {
  112.     schedule();
  113. }
  114.  
  115. void sched_start_task(void) {
  116.     if (!task_list) {
  117.         printf("SCHED: No tasks registered!\n");
  118.         return;
  119.     }
  120.  
  121.     // Set timer
  122.     timer_set(TASK_QUANTUM_MS);
  123.  
  124.     // Start task
  125.     sched_context_restore(&task_list->ctx);
  126. }
  127.  
  128. void schedule(void) {
  129.     task_t *prev = task_list;
  130.     task_t *next = task_list->next;
  131.    
  132.  
  133.  
  134.  
  135.     // Disable interrupts
  136.     __daif_set();
  137.  
  138.     // Locate next task to be run
  139.     while(!(next->task_state & TASK_STATE_READY) && next != task_list) {
  140.         next = next->next;
  141.     }
  142.  
  143.     if (next == prev) {
  144.         // No other task to be run (keep running current task)
  145.         timer_set(TASK_QUANTUM_MS);
  146.  
  147.         // Re-enable interrupts and return
  148.         __daif_clr();
  149.         return;
  150.     }
  151.  
  152.     // Set previous task to ready state
  153.     prev->task_state &= ~(TASK_STATE_RUNNING);
  154.  
  155.     // Update next dask
  156.     task_list = next;
  157.  
  158.     // Set task to running state
  159.     task_list->task_state |= TASK_STATE_RUNNING;
  160.  
  161.     timer_set(TASK_QUANTUM_MS);
  162.     sched_context_switch(&prev->ctx, &next->ctx);
  163. }
Advertisement
Add Comment
Please, Sign In to add comment