/* Copyright (C) Charles Forsyth * See /doc/license/NOTICE.Plan9-9k.txt for details about the licensing. */ /* Portions of this file are Copyright (C) 2015-2018 Giacomo Tesio * See /doc/license/gpl-2.0.txt for details about the licensing. */ typedef struct Alarms Alarms; typedef struct AwakeAlarm AwakeAlarm; typedef struct Block Block; typedef struct Bpool Bpool; typedef struct Chan Chan; typedef struct Cmdbuf Cmdbuf; typedef struct Cmdtab Cmdtab; typedef struct Dev Dev; typedef struct DevConf DevConf; typedef struct Dirtab Dirtab; typedef struct Egrp Egrp; typedef struct Evalue Evalue; typedef struct Fgrp Fgrp; typedef struct Ldseg Ldseg; typedef struct Log Log; typedef struct Logflag Logflag; typedef struct Mntcache Mntcache; typedef struct Mount Mount; typedef struct Mntrpc Mntrpc; typedef struct Mntwalk Mntwalk; typedef struct Mnt Mnt; typedef struct Mhead Mhead; typedef struct Next Next; typedef struct Note Note; typedef struct Path Path; typedef struct Perf Perf; typedef struct PhysUart PhysUart; typedef struct Pgrp Pgrp; typedef struct Proc Proc; typedef struct Procalloc Procalloc; typedef struct Profbuf Profbuf; typedef struct Pte Pte; typedef struct QLock QLock; typedef struct Queue Queue; typedef struct Ref Ref; typedef struct Rendez Rendez; typedef struct Rgrp Rgrp; typedef struct RWlock RWlock; typedef struct Schedq Schedq; typedef struct Sema Sema; typedef struct Timer Timer; typedef struct Timers Timers; typedef struct Uart Uart; typedef struct Waitq Waitq; typedef struct Walkqid Walkqid; typedef struct Watchdog Watchdog; typedef int Devgen(Chan*, char*, Dirtab*, int, int, Dir*); typedef struct Pool Pool; #pragma incomplete Bpool #pragma incomplete DevConf #pragma incomplete Mntrpc #pragma incomplete Queue #pragma incomplete Timers #include <9P2000.h> struct Ref { int ref; }; struct Rendez { Lock l; Proc* p; }; struct QLock { Lock use; /* to access Qlock structure */ Proc* head; /* next process waiting for object */ Proc* tail; /* last process waiting for object */ uintptr_t qpc; /* pc of the holder */ int locked; /* flag */ }; struct RWlock { Lock use; Proc* head; /* list of waiting processes */ Proc* tail; uintptr_t wpc; /* pc of writer */ Proc* wproc; /* writing proc */ int readers; /* number of readers */ int writer; /* number of writers */ }; struct Alarms { QLock ql; Proc *head; }; /* * Access types in namec & channel flags */ enum { Aaccess, /* as in stat, wstat */ Abind, /* for left-hand-side of bind */ Atodir, /* as in chdir */ Aopen, /* for i/o */ Amount, /* to be mounted or mounted upon */ Acreate, /* is to be created */ Aremove, /* will be removed by caller */ COPEN = 0x0001, /* for i/o */ CMSG = 0x0002, /* the message channel for a mount */ /*rsc CCREATE = 0x0004,*/ /* permits creation if c->mnt */ CCEXEC = 0x0008, /* close on exec */ CFREE = 0x0010, /* not in use */ CRCLOSE = 0x0020, /* remove on close */ }; /* flag values */ enum { BINTR = (1<<0), Bipck = (1<<2), /* ip checksum */ Budpck = (1<<3), /* udp checksum */ Btcpck = (1<<4), /* tcp checksum */ Bpktck = (1<<5), /* packet checksum */ }; struct Block { Block* next; Block* list; uint8_t* rp; /* first unconsumed byte */ uint8_t* wp; /* first empty byte */ uint8_t* lim; /* 1 past the end of the buffer */ uint8_t* base; /* start of the buffer */ void (*free)(Block*); uint8_t auxspc[64]; uint16_t flag; uint16_t checksum; /* IP checksum of complete packet (minus media header) */ uint16_t vlan; }; #define BLEN(s) ((s)->wp - (s)->rp) #define BALLOC(s) ((s)->lim - (s)->base) struct Chan { Lock l; Ref r; Chan* next; /* allocation */ Chan* link; int64_t offset; /* in fd */ int64_t devoffset; /* in underlying device; see read */ Dev* dev; uint32_t devno; uint64_t mode; /* read/write */ uint16_t flag; Qid qid; int fid; /* for devmnt */ uint32_t iounit; /* chunk size for i/o; 0==default */ Mhead* umh; /* mount point that derived Chan; used in unionread */ Chan* umc; /* channel in union; held for union read */ QLock umqlock; /* serialize unionreads */ int uri; /* union read index */ int dri; /* devdirread index */ uint8_t* dirrock; /* directory entry rock for translations */ int nrock; int mrock; QLock rockqlock; int ismtpt; Mnt* mux; /* Mnt for clients using me for messages */ union { void* aux; Qid pgrpid; /* for #p/notepg */ uint32_t mid; /* for ns in devproc */ }; Chan* mchan; /* channel to mounted server */ Qid mqid; /* qid of root of mount point */ Path* path; }; struct Path { Ref r; char* s; Chan** mtpt; /* mtpt history */ int len; /* strlen(s) */ int alen; /* allocated length of s */ int mlen; /* number of path elements */ int malen; /* allocated length of mtpt */ }; struct Dev { int dc; char* name; void (*reset)(void); void (*init)(void); void (*shutdown)(void); Chan* (*attach)(Chan *c, Chan *ac, char *spec, int flags); Walkqid*(*walk)(Chan*, Chan*, char**, int); long (*stat)(Chan*, uint8_t*, long); Chan* (*open)(Chan*, unsigned long); Chan* (*create)(Chan*, char*, unsigned long, unsigned long); void (*close)(Chan*); long (*read)(Chan*, void*, long, int64_t); Block* (*bread)(Chan*, long, int64_t); long (*write)(Chan*, void*, long, int64_t); long (*bwrite)(Chan*, Block*, int64_t); void (*remove)(Chan*); long (*wstat)(Chan*, uint8_t*, long); void (*power)(int); /* power mgt: power(1) => on, power (0) => off */ int (*config)(int, char*, DevConf*); /* returns 0 on error */ }; struct Dirtab { char name[KNAMELEN]; Qid qid; int64_t length; long perm; }; struct Walkqid { Chan *clone; int nqid; Qid qid[1]; }; enum { NSMAX = 1000, NSLOG = 7, NSCACHE = (1<ref; channels on this mount point incref(c->mchan) == Mnt.c */ Chan* c; /* Channel to file service */ Proc* rip; /* Reader in progress */ Mntrpc* queue; /* Queue of pending requests on this channel */ uint32_t id; /* Multiplexer id for channel check */ Mnt* list; /* Free list */ int msize; /* data + IOHDRSZ */ char* version; /* 9P version */ Queue* q; /* input queue */ }; enum { NUser, /* note provided externally */ NExit, /* deliver note quietly */ NDebug, /* print debug message */ }; struct Note { char msg[ERRMAX]; int flag; /* whether system posted it */ }; enum { PG_NOFLUSH = 0, PG_TXTFLUSH = 1, /* flush dcache and invalidate icache */ PG_DATFLUSH = 2, /* flush both i & d caches (UNUSED) */ }; #define pagesize(p) (1<<(p)->lg2size) struct Profbuf { Ref r; uint64_t* ticks; /* Tick profile area */ }; struct Sema { Rendez rend; int* addr; int waiting; Sema* next; Sema* prev; }; #include "../port/umem/umem.h" /* demand loading params of a segment */ struct Ldseg { int64_t memsz; int64_t filesz; int64_t pg0fileoff; uintptr_t pg0vaddr; uint32_t pg0off; uint32_t pgsz; SegmentType type : 3; SegPermission permissions : 3; SegFlag flags : 2; }; enum { RENDHASH = 31, /* Hash to lookup rendezvous tags */ MNTLOG = 5, MNTHASH = 1<rendhash[(uintptr_t)(s)%RENDHASH]) #define MOUNTH(p,qid) ((p)->mnthash[(qid).path&((1< variadic */ }; /* * routines to access UART hardware */ struct PhysUart { char* name; Uart* (*pnp)(void); void (*enable)(Uart*, int); void (*disable)(Uart*); void (*kick)(Uart*); void (*dobreak)(Uart*, int); int (*baud)(Uart*, int); int (*bits)(Uart*, int); int (*stop)(Uart*, int); int (*parity)(Uart*, int); void (*modemctl)(Uart*, int); void (*rts)(Uart*, int); void (*dtr)(Uart*, int); long (*status)(Uart*, void*, long, long); void (*fifo)(Uart*, int); void (*power)(Uart*, int); int (*getc)(Uart*); /* polling version for rdb */ void (*putc)(Uart*, int); /* polling version for iprint */ }; enum { Stagesize= 2048 }; /* * software UART */ struct Uart { void* regs; /* hardware stuff */ void* saveregs; /* place to put registers on power down */ char* name; /* internal name */ uint32_t freq; /* clock frequency */ int bits; /* bits per character */ int stop; /* stop bits */ int parity; /* even, odd or no parity */ int baud; /* baud rate */ PhysUart*phys; int console; /* used as a serial console */ int special; /* internal kernel device */ Uart* next; /* list of allocated uarts */ QLock ql; int type; /* ?? */ int dev; int opens; int enabled; Uart *elist; /* next enabled interface */ int perr; /* parity errors */ int ferr; /* framing errors */ int oerr; /* rcvr overruns */ int berr; /* no input buffers */ int serr; /* input queue overflow */ /* buffers */ int (*putc)(Queue*, int); Queue *iq; Queue *oq; Lock rlock; uint8_t istage[Stagesize]; uint8_t *iw; uint8_t *ir; uint8_t *ie; Lock tlock; /* transmit */ uint8_t ostage[Stagesize]; uint8_t *op; uint8_t *oe; int drain; int modem; /* hardware flow control on */ int xonoff; /* software flow control on */ int blocked; int cts, dsr, dcd; /* keep track of modem status */ int ctsbackoff; int hup_dsr, hup_dcd; /* send hangup upstream? */ int dohup; Rendez r; }; extern Uart* consuart; /* * performance timers, all units in perfticks */ struct Perf { uint64_t intrts; /* time of last interrupt */ uint64_t inintr; /* time since last clock tick in interrupt handlers */ uint64_t avg_inintr; /* avg time per clock tick in interrupt handlers */ uint64_t inidle; /* time since last clock tick in idle loop */ uint64_t avg_inidle; /* avg time per clock tick in idle loop */ uint64_t last; /* value of perfticks() at last clock tick */ uint64_t period; /* perfticks() per clock tick */ }; struct Watchdog { void (*enable)(void); /* watchdog enable */ void (*disable)(void); /* watchdog disable */ void (*restart)(void); /* watchdog restart */ void (*stat)(char*, char*); /* watchdog statistics */ }; /* queue state bits, Qmsg, Qcoalesce, and Qkick can be set in qopen */ enum { /* Queue.state */ Qstarve = (1<<0), /* consumer starved */ Qmsg = (1<<1), /* message stream */ Qclosed = (1<<2), /* queue has been closed/hungup */ Qflow = (1<<3), /* producer flow controlled */ Qcoalesce = (1<<4), /* coalesce packets on read */ Qkick = (1<<5), /* always call the kick routine after qwrite */ }; #define DEVDOTDOT -1 #pragma varargck type "I" uint8_t* #pragma varargck type "V" uint8_t* #pragma varargck type "E" uint8_t* #pragma varargck type "M" uint8_t* #pragma varargck type "m" Mreg #pragma varargck type "P" uintmem