toaruos/kernel/include/system.h

285 lines
8.9 KiB
C
Raw Normal View History

2011-12-11 03:45:04 +04:00
/* vim: tabstop=4 shiftwidth=4 noexpandtab
*/
2011-01-16 04:01:19 +03:00
#ifndef __SYSTEM_H
#define __SYSTEM_H
2011-03-22 07:57:24 +03:00
#define _KERNEL_
#include <types.h>
2011-04-17 05:01:04 +04:00
#include <fs.h>
2011-12-15 05:19:51 +04:00
#include <va_list.h>
#include <list.h>
2012-02-06 00:16:59 +04:00
#include <task.h>
#include <process.h>
2012-02-16 06:50:31 +04:00
#define STR(x) #x
#define STRSTR(x) STR(x)
#define asm __asm__
#define volatile __volatile__
2012-01-11 05:54:05 +04:00
extern unsigned int __irq_sem;
#define IRQ_OFF { asm volatile ("cli"); }
#define IRQ_RES { asm volatile ("sti"); }
#define PAUSE { asm volatile ("hlt"); }
#define STOP while (1) { PAUSE; }
2011-10-27 04:11:05 +04:00
#define SYSCALL_VECTOR 0x7F
2012-02-16 06:50:31 +04:00
#define SIGNAL_RETURN 0xFFFFDEAF
#define THREAD_RETURN 0xFFFFB00F
2012-01-11 05:14:30 +04:00
extern void * code;
extern void * end;
extern char * boot_arg; /* Argument to pass to init */
extern char * boot_arg_extra; /* Extra data to pass to init */
2012-01-11 05:14:30 +04:00
2011-01-31 02:00:38 +03:00
extern void *sbrk(uintptr_t increment);
2011-12-11 05:15:12 +04:00
extern void spin_lock(uint8_t volatile * lock);
extern void spin_unlock(uint8_t volatile * lock);
extern void return_to_userspace(void);
2011-01-16 04:01:19 +03:00
/* Kernel Main */
extern int max(int,int);
2011-12-27 05:23:58 +04:00
extern int min(int,int);
extern int abs(int);
extern void swap(int *, int *);
2011-01-31 02:00:38 +03:00
extern void *memcpy(void *restrict dest, const void *restrict src, size_t count);
extern void *memmove(void *restrict dest, const void *restrict src, size_t count);
2011-01-18 03:22:48 +03:00
extern void *memset(void *dest, int val, size_t count);
2011-01-16 04:01:19 +03:00
extern unsigned short *memsetw(unsigned short *dest, unsigned short val, int count);
2011-11-18 01:55:59 +04:00
extern uint32_t strlen(const char *str);
2012-12-01 06:26:47 +04:00
extern char * strdup(const char *str);
extern char * strcpy(char * dest, const char * src);
2011-02-11 06:45:29 +03:00
extern int atoi(const char *str);
2011-01-31 02:00:38 +03:00
extern unsigned char inportb(unsigned short _port);
extern void outportb(unsigned short _port, unsigned char _data);
extern unsigned short inports(unsigned short _port);
extern void outports(unsigned short _port, unsigned short _data);
extern unsigned int inportl(unsigned short _port);
extern void outportl(unsigned short _port, unsigned int _data);
2011-12-13 10:48:04 +04:00
extern void outportsm(unsigned short port, unsigned char * data, unsigned long size);
extern void inportsm(unsigned short port, unsigned char * data, unsigned long size);
extern int strcmp(const char *a, const char *b);
extern char * strtok_r(char * str, const char * delim, char ** saveptr);
extern size_t lfind(const char * str, const char accept);
extern size_t rfind(const char * str, const char accept);
extern size_t strspn(const char * str, const char * accept);
extern char * strpbrk(const char * str, const char * accept);
2013-06-06 10:10:36 +04:00
extern uint32_t krand(void);
2011-11-27 01:25:59 +04:00
extern char * strstr(const char * haystack, const char * needle);
extern uint8_t startswith(const char * str, const char * accept);
2011-01-16 04:01:19 +03:00
2011-01-16 04:59:11 +03:00
/* GDT */
2013-06-06 10:10:36 +04:00
extern void gdt_install(void);
extern void gdt_set_gate(size_t num, unsigned long base, unsigned long limit, unsigned char access, unsigned char gran);
2011-03-30 06:08:56 +04:00
extern void set_kernel_stack(uintptr_t stack);
2011-01-16 04:59:11 +03:00
2011-01-16 06:41:17 +03:00
/* IDT */
2013-06-06 10:10:36 +04:00
extern void idt_install(void);
extern void idt_set_gate(unsigned char num, void (*base)(void), unsigned short sel, unsigned char flags);
2011-01-16 06:41:17 +03:00
/* Registers */
struct regs {
2011-01-31 02:00:38 +03:00
unsigned int gs, fs, es, ds;
unsigned int edi, esi, ebp, esp, ebx, edx, ecx, eax;
unsigned int int_no, err_code;
unsigned int eip, cs, eflags, useresp, ss;
2011-01-16 06:41:17 +03:00
};
2012-02-08 12:40:44 +04:00
typedef struct regs regs_t;
2011-01-31 02:00:38 +03:00
typedef void (*irq_handler_t) (struct regs *);
2011-01-18 03:22:48 +03:00
2011-03-30 11:16:30 +04:00
/* Panic */
#define HALT_AND_CATCH_FIRE(mesg, regs) halt_and_catch_fire(mesg, __FILE__, __LINE__, regs)
#define assert(statement) ((statement) ? (void)0 : assert_failed(__FILE__, __LINE__, #statement))
void halt_and_catch_fire(char *error_message, const char *file, int line, struct regs * regs);
void assert_failed(const char *file, uint32_t line, const char *desc);
2011-01-16 19:56:44 +03:00
/* ISRS */
2013-06-06 10:10:36 +04:00
extern void isrs_install(void);
extern void isrs_install_handler(size_t isrs, irq_handler_t);
extern void isrs_uninstall_handler(size_t isrs);
2011-01-16 19:56:44 +03:00
2011-01-16 07:17:42 +03:00
/* Interrupt Handlers */
2013-06-06 10:10:36 +04:00
extern void irq_install(void);
extern void irq_install_handler(size_t irq, irq_handler_t);
extern void irq_uninstall_handler(size_t irq);
extern void irq_gates(void);
extern void irq_ack(size_t);
2011-01-16 07:17:42 +03:00
2011-01-16 19:56:44 +03:00
/* Timer */
2013-06-06 10:10:36 +04:00
extern void timer_install(void);
2012-12-08 07:06:43 +04:00
extern unsigned long timer_ticks;
extern unsigned char timer_subticks;
2012-12-11 08:28:31 +04:00
extern void relative_time(unsigned long seconds, unsigned long subseconds, unsigned long * out_seconds, unsigned long * out_subseconds);
2011-01-16 19:56:44 +03:00
2011-01-16 21:45:51 +03:00
/* Keyboard */
2013-06-06 10:10:36 +04:00
extern void keyboard_install(void);
extern void keyboard_reset_ps2(void);
extern void keyboard_wait(void);
extern void putch(unsigned char c);
2011-01-16 21:45:51 +03:00
2011-10-30 05:33:24 +04:00
/* Mouse */
2013-06-06 10:10:36 +04:00
extern void mouse_install(void);
2011-10-30 05:33:24 +04:00
2011-01-17 19:24:55 +03:00
/* kprintf */
2011-12-15 05:19:51 +04:00
extern size_t vasprintf(char * buf, const char *fmt, va_list args);
extern int kprintf(const char *fmt, ...);
extern short kprint_to_serial;
extern short kprint_to_screen;
extern void * kprint_to_file;
extern int sprintf(char *buf, const char *fmt, ...);
2011-01-17 19:24:55 +03:00
2011-01-21 04:49:35 +03:00
/* Memory Management */
extern uintptr_t placement_pointer;
2011-01-22 10:38:25 +03:00
extern void kmalloc_startat(uintptr_t address);
2011-01-21 04:49:35 +03:00
extern uintptr_t kmalloc_real(size_t size, int align, uintptr_t * phys);
extern uintptr_t kmalloc(size_t size);
extern uintptr_t kvmalloc(size_t size);
extern uintptr_t kmalloc_p(size_t size, uintptr_t * phys);
extern uintptr_t kvmalloc_p(size_t size, uintptr_t * phys);
2012-02-06 00:16:59 +04:00
// Page types moved to task.h
2011-01-21 04:49:35 +03:00
2014-03-10 10:31:13 +04:00
extern page_directory_t *kernel_directory;
extern page_directory_t *current_directory;
2011-01-21 04:49:35 +03:00
extern void paging_install(uint32_t memsize);
2011-01-31 02:00:38 +03:00
extern void switch_page_directory(page_directory_t * new);
extern page_t *get_page(uintptr_t address, int make, page_directory_t * dir);
2011-01-21 04:49:35 +03:00
extern void page_fault(struct regs *r);
extern void dma_frame(page_t * page, int, int, uintptr_t);
2013-06-06 10:10:36 +04:00
extern void debug_print_directory(void);
2011-01-21 04:49:35 +03:00
2013-12-13 11:40:52 +04:00
int debug_shell_start(void);
2013-06-06 10:10:36 +04:00
void heap_install(void);
2011-01-21 04:49:35 +03:00
void alloc_frame(page_t *page, int is_kernel, int is_writeable);
2011-04-09 02:53:52 +04:00
void free_frame(page_t *page);
2013-06-06 10:10:36 +04:00
uintptr_t memory_use(void);
uintptr_t memory_total(void);
/* klmalloc */
void * __attribute__ ((malloc)) malloc(size_t size);
2011-01-31 02:00:38 +03:00
void * __attribute__ ((malloc)) realloc(void *ptr, size_t size);
void * __attribute__ ((malloc)) calloc(size_t nmemb, size_t size);
2011-02-28 07:00:08 +03:00
void * __attribute__ ((malloc)) valloc(size_t size);
2011-01-31 02:00:38 +03:00
void free(void *ptr);
2011-01-21 04:49:35 +03:00
/* shell */
2013-06-06 10:10:36 +04:00
extern void start_shell(void);
2011-02-11 06:45:29 +03:00
/* Serial */
2013-06-06 10:10:36 +04:00
extern void serial_install(void);
extern int serial_rcvd(int device);
extern char serial_recv(int device);
extern char serial_recv_async(int device);
extern void serial_send(int device, char out);
2013-12-16 09:33:46 +04:00
extern void serial_string(char * out);
2011-02-11 06:45:29 +03:00
/* Tasks */
2013-06-06 10:10:36 +04:00
extern uintptr_t read_eip(void);
extern void copy_page_physical(uint32_t, uint32_t);
extern page_directory_t * clone_directory(page_directory_t * src);
extern page_table_t * clone_table(page_table_t * src, uintptr_t * physAddr);
extern void move_stack(void *new_stack_start, size_t size);
extern void kexit(int retval);
extern void task_exit(int retval);
2011-04-13 00:48:00 +04:00
extern uint32_t next_pid;
2011-03-30 06:08:56 +04:00
2013-06-06 10:10:36 +04:00
extern void tasking_install(void);
extern void switch_task(uint8_t reschedule);
2013-06-06 10:10:36 +04:00
extern void switch_next(void);
extern uint32_t fork(void);
2012-02-16 08:56:16 +04:00
extern uint32_t clone(uintptr_t new_stack, uintptr_t thread_func, uintptr_t arg);
2013-06-06 10:10:36 +04:00
extern uint32_t getpid(void);
2011-04-18 02:44:29 +04:00
extern void enter_user_jmp(uintptr_t location, int argc, char ** argv, uintptr_t stack);
2014-03-10 10:31:13 +04:00
extern uintptr_t initial_esp;
2011-02-11 06:45:29 +03:00
/* Kernel Argument Parser */
extern void parse_args(char * argv);
2011-03-25 08:09:23 +03:00
/* CMOS */
extern void get_time(uint16_t * hours, uint16_t * minutes, uint16_t * seconds);
extern void get_date(uint16_t * month, uint16_t * day);
2011-03-25 08:09:23 +03:00
struct timeval {
uint32_t tv_sec;
uint32_t tv_usec;
};
extern int gettimeofday(struct timeval * t, void * z);
2013-06-06 10:10:36 +04:00
extern uint32_t now(void);
/* CPU Detect by Brynet */
2013-06-06 10:10:36 +04:00
extern int detect_cpu(void);
/* Video Drivers */
/* Generic (pre-set, 32-bit, linear frame buffer) */
extern void graphics_install_preset(uint16_t, uint16_t);
extern uint16_t lfb_resolution_x;
extern uint16_t lfb_resolution_y;
extern uint16_t lfb_resolution_b;
2013-06-06 10:10:36 +04:00
extern uintptr_t lfb_get_address(void);
2012-12-03 11:28:29 +04:00
extern uint8_t * lfb_vid_memory;
/* BOCHS / QEMU VBE Driver */
extern void graphics_install_bochs(uint16_t, uint16_t);
extern void bochs_set_y_offset(uint16_t y);
2013-06-06 10:10:36 +04:00
extern uint16_t bochs_current_scroll(void);
/* Floating Point Unit */
2013-06-06 10:10:36 +04:00
void switch_fpu(void);
2013-12-16 09:33:46 +04:00
void fpu_install(void);
/* ELF */
int exec( char *, int, char **, char **);
2011-04-16 02:33:19 +04:00
int system( char *, int, char **);
2011-03-30 06:08:56 +04:00
/* Sytem Calls */
2013-06-06 10:10:36 +04:00
void syscalls_install(void);
2011-03-30 06:08:56 +04:00
/* PCI */
uint16_t pci_read_word(uint32_t bus, uint32_t slot, uint32_t func, uint16_t offset);
void pci_write_word(uint32_t bus, uint32_t slot, uint32_t func, uint16_t offset, uint32_t data);
2011-10-26 08:33:14 +04:00
/* IDE / PATA */
void ide_init(uint16_t bus);
2011-11-01 02:41:16 +04:00
void ide_read_sector(uint16_t bus, uint8_t slave, uint32_t lba, uint8_t * buf);
void ide_write_sector(uint16_t bus, uint8_t slave, uint32_t lba, uint8_t * buf);
void ide_write_sector_retry(uint16_t bus, uint8_t slave, uint32_t lba, uint8_t * buf);
2011-10-26 08:33:14 +04:00
/* wakeup queue */
int wakeup_queue(list_t * queue);
int sleep_on(list_t * queue);
2012-02-08 12:40:44 +04:00
typedef struct {
uint32_t signum;
uintptr_t handler;
regs_t registers_before;
} signal_t;
2012-02-09 02:07:54 +04:00
void handle_signal(process_t *, signal_t *);
int send_signal(pid_t process, uint32_t signal);
#define USER_STACK_BOTTOM 0xAFF00000
2013-04-25 07:20:34 +04:00
#define USER_STACK_TOP 0xB0000000
#define SHM_START 0xB0000000
void validate(void * ptr);
2013-03-18 11:52:12 +04:00
int validate_safe(void * ptr);
2012-02-17 01:51:26 +04:00
#include <errno_defs.h>
2012-02-17 01:51:26 +04:00
#endif