📄 devarch.c
字号:
#include "u.h"#include "../port/lib.h"#include "mem.h"#include "dat.h"#include "fns.h"#include "io.h"#include "ureg.h"#include "../port/error.h"typedef struct IOMap IOMap;struct IOMap{ IOMap *next; int reserved; char tag[13]; ulong start; ulong end;};static struct{ Lock; IOMap *m; IOMap *free; IOMap maps[32]; // some initial free maps QLock ql; // lock for reading map} iomap;enum { Qdir = 0, Qioalloc = 1, Qiob, Qiow, Qiol, Qbase, Qmax = 16,};typedef long Rdwrfn(Chan*, void*, long, vlong);static Rdwrfn *readfn[Qmax];static Rdwrfn *writefn[Qmax];static Dirtab archdir[Qmax] = { ".", { Qdir, 0, QTDIR }, 0, 0555, "ioalloc", { Qioalloc, 0 }, 0, 0444, "iob", { Qiob, 0 }, 0, 0660, "iow", { Qiow, 0 }, 0, 0660, "iol", { Qiol, 0 }, 0, 0660,};Lock archwlock; /* the lock is only for changing archdir */int narchdir = Qbase;int (*_pcmspecial)(char*, ISAConf*);void (*_pcmspecialclose)(int);static int doi8253set = 1;/* * Add a file to the #P listing. Once added, you can't delete it. * You can't add a file with the same name as one already there, * and you get a pointer to the Dirtab entry so you can do things * like change the Qid version. Changing the Qid path is disallowed. */Dirtab*addarchfile(char *name, int perm, Rdwrfn *rdfn, Rdwrfn *wrfn){ int i; Dirtab d; Dirtab *dp; memset(&d, 0, sizeof d); strcpy(d.name, name); d.perm = perm; lock(&archwlock); if(narchdir >= Qmax){ unlock(&archwlock); return nil; } for(i=0; i<narchdir; i++) if(strcmp(archdir[i].name, name) == 0){ unlock(&archwlock); return nil; } d.qid.path = narchdir; archdir[narchdir] = d; readfn[narchdir] = rdfn; writefn[narchdir] = wrfn; dp = &archdir[narchdir++]; unlock(&archwlock); return dp;}voidioinit(void){ char *excluded; int i; for(i = 0; i < nelem(iomap.maps)-1; i++) iomap.maps[i].next = &iomap.maps[i+1]; iomap.maps[i].next = nil; iomap.free = iomap.maps; /* * This is necessary to make the IBM X20 boot. * Have not tracked down the reason. */ ioalloc(0x0fff, 1, 0, "dummy"); // i82557 is at 0x1000, the dummy // entry is needed for swappable devs. if ((excluded = getconf("ioexclude")) != nil) { char *s; s = excluded; while (s && *s != '\0' && *s != '\n') { char *ends; int io_s, io_e; io_s = (int)strtol(s, &ends, 0); if (ends == nil || ends == s || *ends != '-') { print("ioinit: cannot parse option string\n"); break; } s = ++ends; io_e = (int)strtol(s, &ends, 0); if (ends && *ends == ',') *ends++ = '\0'; s = ends; ioalloc(io_s, io_e - io_s + 1, 0, "pre-allocated"); } }}// Reserve a range to be ioalloced later. // This is in particular useful for exchangable cards, such// as pcmcia and cardbus cards.intioreserve(int, int size, int align, char *tag){ IOMap *m, **l; int i, port; lock(&iomap); // find a free port above 0x400 and below 0x1000 port = 0x400; for(l = &iomap.m; *l; l = &(*l)->next){ m = *l; if (m->start < 0x400) continue; i = m->start - port; if(i > size) break; if(align > 0) port = ((port+align-1)/align)*align; else port = m->end; } if(*l == nil){ unlock(&iomap); return -1; } m = iomap.free; if(m == nil){ print("ioalloc: out of maps"); unlock(&iomap); return port; } iomap.free = m->next; m->next = *l; m->start = port; m->end = port + size; m->reserved = 1; strncpy(m->tag, tag, sizeof(m->tag)); m->tag[sizeof(m->tag)-1] = 0; *l = m; archdir[0].qid.vers++; unlock(&iomap); return m->start;}//// alloc some io port space and remember who it was// alloced to. if port < 0, find a free region.//intioalloc(int port, int size, int align, char *tag){ IOMap *m, **l; int i; lock(&iomap); if(port < 0){ // find a free port above 0x400 and below 0x1000 port = 0x400; for(l = &iomap.m; *l; l = &(*l)->next){ m = *l; if (m->start < 0x400) continue; i = m->start - port; if(i > size) break; if(align > 0) port = ((port+align-1)/align)*align; else port = m->end; } if(*l == nil){ unlock(&iomap); return -1; } } else { // Only 64KB I/O space on the x86. if((port+size) > 0x10000){ unlock(&iomap); return -1; } // see if the space clashes with previously allocated ports for(l = &iomap.m; *l; l = &(*l)->next){ m = *l; if(m->end <= port) continue; if(m->reserved && m->start == port && m->end == port + size) { m->reserved = 0; unlock(&iomap); return m->start; } if(m->start >= port+size) break; unlock(&iomap); return -1; } } m = iomap.free; if(m == nil){ print("ioalloc: out of maps"); unlock(&iomap); return port; } iomap.free = m->next; m->next = *l; m->start = port; m->end = port + size; strncpy(m->tag, tag, sizeof(m->tag)); m->tag[sizeof(m->tag)-1] = 0; *l = m; archdir[0].qid.vers++; unlock(&iomap); return m->start;}voidiofree(int port){ IOMap *m, **l; lock(&iomap); for(l = &iomap.m; *l; l = &(*l)->next){ if((*l)->start == port){ m = *l; *l = m->next; m->next = iomap.free; iomap.free = m; break; } if((*l)->start > port) break; } archdir[0].qid.vers++; unlock(&iomap);}intiounused(int start, int end){ IOMap *m; for(m = iomap.m; m; m = m->next){ if(start >= m->start && start < m->end || start <= m->start && end > m->start) return 0; } return 1;}static voidcheckport(int start, int end){ /* standard vga regs are OK */ if(start >= 0x2b0 && end <= 0x2df+1) return; if(start >= 0x3c0 && end <= 0x3da+1) return; if(iounused(start, end)) return; error(Eperm);}static Chan*archattach(char* spec){ return devattach('P', spec);}Walkqid*archwalk(Chan* c, Chan *nc, char** name, int nname){ return devwalk(c, nc, name, nname, archdir, narchdir, devgen);}static intarchstat(Chan* c, uchar* dp, int n){ return devstat(c, dp, n, archdir, narchdir, devgen);}static Chan*archopen(Chan* c, int omode){ return devopen(c, omode, archdir, narchdir, devgen);}static voidarchclose(Chan*){}enum{ Linelen= 31,};static longarchread(Chan *c, void *a, long n, vlong offset){ char *buf, *p; int port; ushort *sp; ulong *lp; IOMap *m; Rdwrfn *fn; switch((ulong)c->qid.path){ case Qdir: return devdirread(c, a, n, archdir, narchdir, devgen); case Qiob: port = offset; checkport(offset, offset+n); for(p = a; port < offset+n; port++) *p++ = inb(port); return n; case Qiow: if(n & 1) error(Ebadarg); checkport(offset, offset+n); sp = a; for(port = offset; port < offset+n; port += 2) *sp++ = ins(port); return n; case Qiol: if(n & 3) error(Ebadarg); checkport(offset, offset+n); lp = a; for(port = offset; port < offset+n; port += 4) *lp++ = inl(port); return n; case Qioalloc: break; default: if(c->qid.path < narchdir && (fn = readfn[c->qid.path])) return fn(c, a, n, offset); error(Eperm); break; } if((buf = malloc(n)) == nil) error(Enomem); p = buf; n = n/Linelen; offset = offset/Linelen; lock(&iomap); for(m = iomap.m; n > 0 && m != nil; m = m->next){ if(offset-- > 0) continue; sprint(p, "%8lux %8lux %-12.12s\n", m->start, m->end-1, m->tag); p += Linelen; n--; } unlock(&iomap); n = p - buf; memmove(a, buf, n); free(buf); return n;}static longarchwrite(Chan *c, void *a, long n, vlong offset){ char *p; int port; ushort *sp; ulong *lp; Rdwrfn *fn; switch((ulong)c->qid.path){ case Qiob: p = a; checkport(offset, offset+n); for(port = offset; port < offset+n; port++) outb(port, *p++); return n; case Qiow: if(n & 1) error(Ebadarg); checkport(offset, offset+n); sp = a; for(port = offset; port < offset+n; port += 2) outs(port, *sp++); return n; case Qiol: if(n & 3) error(Ebadarg); checkport(offset, offset+n); lp = a; for(port = offset; port < offset+n; port += 4) outl(port, *lp++); return n; default: if(c->qid.path < narchdir && (fn = writefn[c->qid.path])) return fn(c, a, n, offset); error(Eperm); break; } return 0;}Dev archdevtab = { 'P', "arch", devreset, devinit, devshutdown, archattach, archwalk, archstat, archopen, devcreate, archclose, archread, devbread, archwrite, devbwrite, devremove, devwstat,};/* * the following is a generic version of the * architecture specific stuff */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -