9os

Experimental kernel using plan9 ideas for embedded device
git clone git://git.simple-cc.org/9os
Log | Files | Refs | README | LICENSE

os9.h (6645B)


      1 #include <stddef.h>
      2 #include <stdint.h>
      3 
      4 #include <arch/types.h>
      5 #include <os9/const.h>
      6 
      7 #include "../features.h"
      8 
      9 #define OS9_H
     10 
     11 #ifndef NDEBUG
     12   #define dbg kprint
     13 #else
     14   #define dbg
     15 #endif
     16 
     17 #define KiB 1024u
     18 #define MiB (1024u * KiB)
     19 #define GiB (1024ul * MiB)
     20 
     21 #define VMEM(x) ((void *) ((char *) (x) + mach.kzero))
     22 #define PMEM(x) ((void *) ((char *) (x) - mach.kzero))
     23 #define PAGESIZE (4 * KiB)
     24 #define ENABLE   1
     25 #define DISABLE  0
     26 
     27 #define QID(t, v, p) ((Qid) {.type = (t), .vers = (v), .path = (p)})
     28 
     29 #ifndef NR_BUFFERS
     30 #define NR_BUFFERS 512
     31 #endif
     32 
     33 #ifndef HEAPSIZ
     34 #define HEAPSIZ 4
     35 #endif
     36 
     37 #define SEEK_SET 0
     38 #define SEEK_CUR 1
     39 #define SEEK_END 2
     40 #define EOF -1
     41 
     42 typedef struct ptable Ptable;
     43 typedef struct context Context;
     44 typedef struct task Task;
     45 typedef struct chan Chan;
     46 typedef struct qid Qid;
     47 typedef struct dir Dir;
     48 typedef struct mach Mach;
     49 typedef struct map Map;
     50 typedef struct page Page;
     51 typedef struct mpoint Mpoint;
     52 typedef struct nspace Nspace;
     53 typedef struct fdset Fdset;
     54 typedef struct ref Ref;
     55 typedef struct rendez Rendez;
     56 
     57 enum rforkflags {
     58 	RFPROC = 1 << 0,
     59 	RFNAMEG = 1 << 1,
     60 	RFCNAMEG = 1 << 2,
     61 	RFFDG = 1 <<  3,
     62 	RFCFDG = 1 << 4,
     63 	RFMEM = 1 << 5,
     64 };
     65 
     66 enum barrier_type {
     67 	CODE,
     68 	DATA,
     69 };
     70 
     71 enum devflags {
     72 	O_READ   = 1 << 0,
     73 	O_WRITE  = 1 << 1,
     74 	O_RDWR   = 1 << 2,
     75 	O_BIND   = 1 << 3,
     76 	O_DIR    = 1 << 4,
     77 	O_STAT   = 1 << 5,
     78 };
     79 
     80 enum pstates {
     81 	PSYSTEM = 1 << 0,
     82 };
     83 
     84 enum tflags {
     85 	TLOCK_PREEMP = 1 << 0,
     86 };
     87 
     88 enum tmode {
     89 	TINIT,
     90 	TREADY,
     91 	TRUNNING,
     92 	TWAITING,
     93 	TFAULTED,
     94 	TFREE,
     95 };
     96 
     97 enum map_attr {
     98 	MR = 1 << 0,
     99 	MW = 1 << 1,
    100 	MX = 1 << 2,
    101 	MD = 1 << 3,
    102 };
    103 
    104 enum sysnumbers {
    105 	SYSOPEN,
    106 	NR_SYSCALLS,
    107 };
    108 
    109 struct ref {
    110 	mutex_t m;
    111 	int cnt;
    112 };
    113 
    114 struct page {
    115 	int allocated;
    116 	uintptr_t va;
    117 	phyaddr_t pa;
    118 	struct page *next;
    119 };
    120 
    121 struct map {
    122 	Ref ref;
    123 	char *name;
    124 	unsigned perm;
    125 
    126 	phyaddr_t pa;
    127 	uintptr_t va;
    128 	size_t siz;
    129 
    130 	Page *pages;
    131 
    132 	Map *next;
    133 };
    134 
    135 /*
    136  * phystack and ktzero must be the first two fields of Mach
    137  * because crt.s expects that.
    138  */
    139 struct mach {
    140 	phyaddr_t phystack;
    141 	phyaddr_t phytext;
    142 	phyaddr_t kzero;
    143 
    144 	Map *maps;
    145 };
    146 
    147 struct qid {
    148 	unsigned long long path;
    149 	unsigned long vers;
    150 	unsigned char type;
    151 };
    152 
    153 struct dir {
    154 	char name[NAMELEN];
    155 	long length;
    156 	unsigned char mode;
    157 	unsigned char type;
    158 	unsigned char dev;
    159 	Qid qid;
    160 };
    161 
    162 struct chan {
    163 	Ref ref;
    164 	long offset;
    165 	int index;
    166 	Qid qid;
    167 	unsigned char type;
    168 	unsigned char dev;
    169 	unsigned char mode;
    170 	mutex_t mutex;
    171 	Chan *next;
    172 };
    173 
    174 struct mpoint {
    175 	Ref ref;
    176 	Chan *new;
    177 	Chan *old;
    178 	mutex_t m;
    179 	Mpoint *next;
    180 };
    181 
    182 struct nspace {
    183 	Ref ref;
    184 	mutex_t m;
    185 	Mpoint *mpoints[NR_MPOINTS];
    186 	Nspace *next;
    187 };
    188 
    189 struct fdset {
    190 	Ref ref;
    191 	mutex_t m;
    192 	Chan *fdset[NR_CHANS];
    193 	Fdset *next;
    194 };
    195 
    196 struct rendez {
    197         spinlock_t s;
    198         Task *task;
    199 };
    200 
    201 struct syscall {
    202 	int nargs;
    203 	int (*fn)(int, ...);
    204 };
    205 
    206 /**
    207  * @entry: Entry point for the task.
    208  * @prio:
    209  * @baseprio:
    210  * @retainprio:
    211  * @wait:
    212  * @locklevel: Denotes the current lock level of the task within the
    213  *             process that has preemption locked.
    214  * @affinity: Core mask indicating cores where task can execute.
    215  * @period: Process period.
    216  * @duration: The amount of execution time required by the partition
    217  *            within one partition period.
    218  * @capacity:
    219  * @deadline:
    220  * @deadtime:
    221  * @flags:
    222  * @state:
    223  * @ctx:
    224  * @ptable:
    225  * @text:
    226  * @data:
    227  * @stack:
    228  * @ns:
    229  * @fds:
    230  */
    231 struct task {
    232 	int pid;
    233 	int ppid;
    234 
    235 	void *entry;
    236 
    237 	int errno;
    238 	int ret;
    239 
    240 	int prio;
    241 	int baseprio;
    242 	int retainprio;
    243 
    244 	int wait;
    245 	int locklevel;
    246 	unsigned long long affinity;
    247 
    248 	long period;
    249 	long duration;
    250 	long capacity;
    251 
    252 	long deadline;
    253 	long deadtime;
    254 
    255 	int flags;
    256 	int state;
    257 
    258 	mutex_t m;
    259 
    260 	Context ctx;
    261 	Ptable ptable;
    262 
    263 	Map *text;
    264 	Map *data;
    265 	Map stack;
    266 	Map kstack;
    267 
    268 	Nspace *ns;
    269 	Fdset *fds;
    270 	Chan slash;
    271 
    272 	Task *next;
    273 };
    274 
    275 /* proc.c */
    276 extern void iproc(void);
    277 extern void initref(Ref *);
    278 extern void incref(Ref *);
    279 extern int decref(Ref *);
    280 extern int getntask(int n, Task **);
    281 extern Task *gettask(int);
    282 extern int mapseg(Map *);
    283 extern Task *kproc(void *);
    284 extern Map *newstack(Task *);
    285 extern void sched(void);
    286 extern void locktask(Task *);
    287 extern void unlocktask(Task *);
    288 extern void sleep(Rendez *, int (*cond)(void *), void *);
    289 extern void wakeup(Rendez *);
    290 extern void seterror(int);
    291 
    292 /* alloc.c */
    293 extern void *alloc(size_t);
    294 extern void *allocb(void);
    295 extern Page *allocp(phyaddr_t);
    296 extern Nspace *allocspace(void);
    297 extern Map *allocmap(void);
    298 extern Fdset *allocfds(void);
    299 extern Chan *allocchan(void);
    300 extern Mpoint *allocmpoint(void);
    301 
    302 extern void freeb(void *);
    303 extern void freep(Page *);
    304 extern void freespace(Nspace *);
    305 extern void freemap(Map *);
    306 extern void freefds(Fdset *);
    307 extern void freechan(Chan *);
    308 extern void freempoint(Mpoint *);
    309 extern void ialloc(void);
    310 
    311 /* map.c */
    312 extern Map *newstack(Task *);
    313 extern Map *newkstack(Task *);
    314 extern int unmapseg(Map *);
    315 extern int mapseg(Map *);
    316 extern void delmap(Map *);
    317 extern Map *newmap(Map *);
    318 
    319 /* sys.c */
    320 extern void isys(void);
    321 
    322 /* dlang.c */
    323 extern int debug(void);
    324 
    325 /* arch mmu.c */
    326 extern int vmap(phyaddr_t, uintptr_t, int);
    327 extern int vunmap(phyaddr_t, uintptr_t);
    328 
    329 /* arch functions */
    330 extern Context *getctx(Context *ctx);
    331 extern void interrupt(int);
    332 extern uint8_t inm8(void *addr);
    333 extern uint16_t inm16(void *addr);
    334 extern uint32_t inm32(void *addr);
    335 extern uint8_t outm8(uint8_t, void *addr);
    336 extern uint16_t outm16(uint16_t, void *addr);
    337 extern uint32_t outm32(uint32_t, void *addr);
    338 extern void lock(mutex_t *m);
    339 extern void unlock(mutex_t *m);
    340 extern int trylock(mutex_t *m);
    341 extern void lockspin(spinlock_t *);
    342 extern void unlockspin(spinlock_t *);
    343 extern void barrier(int);
    344 extern noreturn void halt(void);
    345 extern noreturn void panic(const char *msg);
    346 extern noreturn void fault(const char *msg, Context *ctx);
    347 extern noreturn void swtch(Context *ctx);
    348 extern noreturn void trap(Context *ctx);
    349 extern Ptable *initptable(Task *);
    350 extern void ictx(Task *, void *);
    351 extern int dupctx(Task *);
    352 extern char *getconf(char *);
    353 
    354 /* dev functions */
    355 extern void idev(void);
    356 extern Chan *namec(char *, int mode);
    357 extern int chanwrite(Chan *c, void *buf, int n);
    358 extern int chanread(Chan *c, void *buf, int n);
    359 extern int chanclose(Chan *c);
    360 extern int mount(char *, char *, char *);
    361 extern int bind(char *, char *);
    362 extern Nspace *newspace(Nspace *);
    363 extern Fdset *newfds(Fdset *);
    364 extern void delspace(Nspace *);
    365 extern void delfds(Fdset *);
    366 
    367 /* globals */
    368 extern Chan *console;
    369 extern Mach mach;
    370 extern char buffertab[NR_BUFFERS][PAGESIZE];
    371 extern char bufto[], buffrom[];
    372 extern struct syscall systab[];
    373 
    374 /* per cpu globals */
    375 extern Task *proc;
    376 extern int cpuid;
    377 extern long long now;