system.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. */
  3. #pragma once
  4. #include <va_list.h>
  5. #include <kernel/types.h>
  6. #include <kernel/fs.h>
  7. #include <kernel/task.h>
  8. #include <kernel/process.h>
  9. #include <kernel/libc.h>
  10. #include <toaru/list.h>
  11. #define ASSUME(cond) __extension__ ({ if (!(cond)) { __builtin_unreachable(); } })
  12. #define STR(x) #x
  13. #define STRSTR(x) STR(x)
  14. #define asm __asm__
  15. #define volatile __volatile__
  16. void int_disable(void);
  17. void int_resume(void);
  18. void int_enable(void);
  19. #define IRQ_OFF int_disable()
  20. #define IRQ_RES int_resume()
  21. #define IRQ_ON int_enable()
  22. #define PAUSE { asm volatile ("hlt"); }
  23. #define STOP while (1) { PAUSE; }
  24. #define SYSCALL_VECTOR 0x7F
  25. #define SIGNAL_RETURN 0xFFFFDEAF
  26. #define THREAD_RETURN 0xFFFFB00F
  27. extern void * code;
  28. extern void * end;
  29. extern char * boot_arg; /* Argument to pass to init */
  30. extern char * boot_arg_extra; /* Extra data to pass to init */
  31. extern void *sbrk(uintptr_t increment);
  32. /* spin.c */
  33. typedef volatile int spin_lock_t[2];
  34. extern void spin_init(spin_lock_t lock);
  35. extern void spin_lock(spin_lock_t lock);
  36. extern void spin_unlock(spin_lock_t lock);
  37. extern void return_to_userspace(void);
  38. /* Kernel Main */
  39. extern unsigned short *memsetw(unsigned short *dest, unsigned short val, int count);
  40. extern unsigned char inportb(unsigned short _port);
  41. extern void outportb(unsigned short _port, unsigned char _data);
  42. extern unsigned short inports(unsigned short _port);
  43. extern void outports(unsigned short _port, unsigned short _data);
  44. extern unsigned int inportl(unsigned short _port);
  45. extern void outportl(unsigned short _port, unsigned int _data);
  46. extern void outportsm(unsigned short port, unsigned char * data, unsigned long size);
  47. extern void inportsm(unsigned short port, unsigned char * data, unsigned long size);
  48. extern size_t lfind(const char * str, const char accept);
  49. extern size_t rfind(const char * str, const char accept);
  50. extern uint32_t krand(void);
  51. extern uint8_t startswith(const char * str, const char * accept);
  52. /* GDT */
  53. extern void gdt_install(void);
  54. extern void gdt_set_gate(uint8_t num, uint64_t base, uint64_t limit, uint8_t access, uint8_t gran);
  55. extern void set_kernel_stack(uintptr_t stack);
  56. /* IDT */
  57. extern void idt_install(void);
  58. extern void idt_set_gate(uint8_t num, void (*base)(void), uint16_t sel, uint8_t flags);
  59. /* Registers
  60. *
  61. * Note: if the order of these changes, sys/task.S must be changed to use
  62. * the correct offsets as well.
  63. */
  64. struct regs {
  65. unsigned int gs, fs, es, ds;
  66. unsigned int edi, esi, ebp, esp, ebx, edx, ecx, eax;
  67. unsigned int int_no, err_code;
  68. unsigned int eip, cs, eflags, useresp, ss;
  69. };
  70. typedef struct regs regs_t;
  71. typedef void (*irq_handler_t) (struct regs *);
  72. typedef int (*irq_handler_chain_t) (struct regs *);
  73. /* Panic */
  74. #define HALT_AND_CATCH_FIRE(mesg, regs) halt_and_catch_fire(mesg, __FILE__, __LINE__, regs)
  75. #define assert(statement) ((statement) ? (void)0 : assert_failed(__FILE__, __LINE__, #statement))
  76. void halt_and_catch_fire(char *error_message, const char *file, int line, struct regs * regs);
  77. void assert_failed(const char *file, uint32_t line, const char *desc);
  78. /* ISRS */
  79. extern void isrs_install(void);
  80. extern void isrs_install_handler(size_t isrs, irq_handler_t);
  81. extern void isrs_uninstall_handler(size_t isrs);
  82. /* Interrupt Handlers */
  83. extern void irq_install(void);
  84. extern void irq_install_handler(size_t irq, irq_handler_chain_t, char * desc);
  85. extern void irq_uninstall_handler(size_t irq);
  86. extern int irq_is_handler_free(size_t irq);
  87. extern void irq_gates(void);
  88. extern void irq_ack(size_t);
  89. /* Timer */
  90. extern void timer_install(void);
  91. extern unsigned long timer_ticks;
  92. extern unsigned long timer_subticks;
  93. extern signed long timer_drift;
  94. extern void relative_time(unsigned long seconds, unsigned long subseconds, unsigned long * out_seconds, unsigned long * out_subseconds);
  95. /* Memory Management */
  96. extern uintptr_t placement_pointer;
  97. extern void kmalloc_startat(uintptr_t address);
  98. extern uintptr_t kmalloc_real(size_t size, int align, uintptr_t * phys);
  99. extern uintptr_t kmalloc(size_t size);
  100. extern uintptr_t kvmalloc(size_t size);
  101. extern uintptr_t kmalloc_p(size_t size, uintptr_t * phys);
  102. extern uintptr_t kvmalloc_p(size_t size, uintptr_t * phys);
  103. // Page types moved to task.h
  104. extern page_directory_t *kernel_directory;
  105. extern page_directory_t *current_directory;
  106. extern void paging_install(uint32_t memsize);
  107. extern void paging_prestart(void);
  108. extern void paging_finalize(void);
  109. extern void paging_mark_system(uint64_t addr);
  110. extern void switch_page_directory(page_directory_t * new);
  111. extern void invalidate_page_tables(void);
  112. extern void invalidate_tables_at(uintptr_t addr);
  113. extern page_t *get_page(uintptr_t address, int make, page_directory_t * dir);
  114. extern void page_fault(struct regs *r);
  115. extern void dma_frame(page_t * page, int, int, uintptr_t);
  116. extern void debug_print_directory(page_directory_t *);
  117. int debug_shell_start(void);
  118. void heap_install(void);
  119. void alloc_frame(page_t *page, int is_kernel, int is_writeable);
  120. void free_frame(page_t *page);
  121. uintptr_t memory_use(void);
  122. uintptr_t memory_total(void);
  123. /* klmalloc */
  124. void * __attribute__ ((malloc)) malloc(size_t size);
  125. void * __attribute__ ((malloc)) realloc(void *ptr, size_t size);
  126. void * __attribute__ ((malloc)) calloc(size_t nmemb, size_t size);
  127. void * __attribute__ ((malloc)) valloc(size_t size);
  128. void free(void *ptr);
  129. /* Tasks */
  130. extern uintptr_t read_eip(void);
  131. extern void copy_page_physical(uint32_t, uint32_t);
  132. extern page_directory_t * clone_directory(page_directory_t * src);
  133. extern page_table_t * clone_table(page_table_t * src, uintptr_t * physAddr);
  134. extern void move_stack(void *new_stack_start, size_t size);
  135. extern void kexit(int retval);
  136. extern void task_exit(int retval);
  137. extern uint32_t next_pid;
  138. extern void tasking_install(void);
  139. extern void switch_task(uint8_t reschedule);
  140. extern void switch_next(void);
  141. extern uint32_t fork(void);
  142. extern uint32_t clone(uintptr_t new_stack, uintptr_t thread_func, uintptr_t arg);
  143. extern uint32_t getpid(void);
  144. extern void enter_user_jmp(uintptr_t location, int argc, char ** argv, uintptr_t stack);
  145. extern uintptr_t initial_esp;
  146. /* Kernel Argument Parser */
  147. extern void parse_args(char * argv);
  148. /* CMOS */
  149. extern void get_time(uint16_t * hours, uint16_t * minutes, uint16_t * seconds);
  150. extern void get_date(uint16_t * month, uint16_t * day);
  151. extern uint32_t boot_time;
  152. extern uint32_t read_cmos(void);
  153. extern int gettimeofday(struct timeval * t, void * z);
  154. extern uint32_t now(void);
  155. /* Floating Point Unit */
  156. extern void switch_fpu(void);
  157. extern void unswitch_fpu(void);
  158. extern void fpu_install(void);
  159. /* ELF */
  160. extern int exec( char *, int, char **, char **);
  161. extern int system( char *, int, char **);
  162. /* Sytem Calls */
  163. extern void syscalls_install(void);
  164. /* wakeup queue */
  165. extern int wakeup_queue(list_t * queue);
  166. extern int wakeup_queue_interrupted(list_t * queue);
  167. extern int sleep_on(list_t * queue);
  168. typedef struct {
  169. uint32_t signum;
  170. uintptr_t handler;
  171. regs_t registers_before;
  172. } signal_t;
  173. extern void handle_signal(process_t *, signal_t *);
  174. extern int send_signal(pid_t process, uint32_t signal);
  175. #define USER_STACK_BOTTOM 0xAFF00000
  176. #define USER_STACK_TOP 0xB0000000
  177. #define SHM_START 0xB0000000
  178. extern void validate(void * ptr);
  179. extern int validate_safe(void * ptr);
  180. #include <errno.h>