task.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* kernel/task.c
  2. *
  3. * Copyright (C) 2021, 2022 intirain.
  4. *
  5. * This file is part of evx.
  6. *
  7. * evx is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * evx is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with evx. If not, see <https://www.gnu.org/licenses/>
  19. */
  20. #include <kernel/init.h>
  21. #include <kernel/kernel.h>
  22. #include <kernel/module.h>
  23. #include <kernel/mm.h>
  24. #include <kernel/task.h>
  25. #include <kernel/kmlog.h>
  26. #include <arch/archinfo.h>
  27. #include <sys/types.h>
  28. struct task init_task = {
  29. .state = TASK_RUNNING,
  30. .pid = 0,
  31. .parent = NULL,
  32. .arch_descr = ARCH_TASK_DESCR_INIT,
  33. .nice = 0
  34. };
  35. struct task *current = &init_task;
  36. int next_pid = 1;
  37. int task_init(void) {
  38. struct task *new = dup_task();
  39. kmlog("New task: %p\n", new);
  40. kmlog("task initialization finished. \n");
  41. switch_to(new);
  42. return 0;
  43. }
  44. struct task *dup_task(void) {
  45. struct task *tsk;
  46. if (unlikely(!(tsk = kmalloc(sizeof(struct task)))))
  47. panic("dup_task: Out of memory");
  48. tsk->state = TASK_WAIT;
  49. tsk->pid = next_pid++;
  50. tsk->parent = current;
  51. tsk->nice = 0;
  52. arch_task_descr_init(&tsk->arch_descr);
  53. arch_stack_init(&tsk->arch_descr);
  54. tsk->state = TASK_READY;
  55. return tsk;
  56. }
  57. kernel_init(task_init);