commit 31f0ece115a4b80a6fa447626e5226bf826fdea1
parent 622e9826de265563f16e453c4484575c1651da4a
Author: Roberto E. Vargas Caballero <roberto.vargas@midokura.com>
Date: Sat, 19 Nov 2022 10:50:15 +0100
os9: Move functions related to map to map.c
Diffstat:
4 files changed, 142 insertions(+), 130 deletions(-)
diff --git a/include/os9/os9.h b/include/os9/os9.h
@@ -277,6 +277,14 @@ extern void freemap(Map *);
extern void freefds(Fdset *);
extern void ialloc(void);
+/* map.c */
+extern Map *newstack(Task *);
+extern Map *newkstack(Task *);
+extern int unmapseg(Map *);
+extern int mapseg(Map *);
+extern void delmap(Map *);
+extern Map *newmap(Map *);
+
/* sys.c */
extern void isys(void);
extern void info(void);
diff --git a/src/os9/Makefile b/src/os9/Makefile
@@ -11,6 +11,7 @@ include $(PROJECTDIR)/scripts/rules.mk
OBJS =\
alloc.o\
dlang.o\
+ map.o\
proc.o\
sys.o\
dev/builtin.o\
diff --git a/src/os9/map.c b/src/os9/map.c
@@ -0,0 +1,133 @@
+#include <os9/os9.h>
+
+#include <libk.h>
+
+Map *
+newstack(Task *tp)
+{
+ Map *mp = &tp->stack;
+
+ initref(&mp->ref);
+ mp->name = "stack";
+ mp->perm = MW | MR;
+
+ mp->pa = -1;
+ mp->va = STACKADDR & (STACKADDR-1);
+ mp->siz = PAGESIZE;
+
+ if (mapseg(mp) < 0)
+ return NULL;
+
+ return mp;
+}
+
+Map *
+newkstack(Task *tp)
+{
+ Map *mp = &tp->kstack;
+
+ initref(&mp->ref);
+ mp->name = "kstack";
+ mp->perm = MW | MR;
+
+ mp->pa = -1;
+ mp->va = KSTACKADDR & (STACKADDR-1);
+ mp->siz = PAGESIZE;
+
+ if (mapseg(mp) < 0)
+ return NULL;
+
+ return mp;
+}
+
+int
+unmapseg(Map *mp)
+{
+ Page *p;
+
+ dbg("unmap %s: %llx -> %llx\n",
+ (mp->name) ? mp->name : "unnamed",
+ mp->va, mp->pa);
+
+ for (p = mp->pages; p; p = p->next) {
+ vunmap(p->pa, p->va);
+ freep(p);
+ }
+
+ mp->pages = NULL;
+
+ return 0;
+}
+
+int
+mapseg(Map *mp)
+{
+ phyaddr_t pa;
+ uintptr_t va;
+ unsigned long i, n;
+ Page *p;
+
+ assert(mp->perm & MR);
+
+ dbg("map %s: %llx -> %llx %c %c%c%c (%llu)\n",
+ (mp->name) ? mp->name : "unnamed",
+ mp->va, mp->pa,
+ (mp->perm & MD) ? 'd' : 'n',
+ (mp->perm & MR) ? 'r' : '-',
+ (mp->perm & MW) ? 'w' : '-',
+ (mp->perm & MX) ? 'x' : '-',
+ mp->siz);
+
+ n = (mp->siz + PAGESIZE-1)/PAGESIZE;
+ va = mp->va;
+ pa = mp->pa;
+
+ for (i = 0; i < n; i++) {
+ if (mp->pa == -1)
+ pa = -1;
+
+ if ((p = allocp(pa)) == NULL)
+ goto err;
+ p->va = va;
+ p->next = mp->pages;
+
+ if (vmap(p->pa, va, mp->perm) < 0) {
+ freep(p);
+ goto err;
+ }
+
+ mp->pages = p;
+ pa += PAGESIZE;
+ va += PAGESIZE;
+ }
+
+ return 0;
+
+err:
+ unmapseg(mp);
+ return -1;
+}
+
+void
+delmap(Map *mp)
+{
+ if (!decref(&mp->ref))
+ return;
+ unmapseg(mp);
+ freemap(mp);
+}
+
+Map *
+newmap(Map *from)
+{
+ Map *mp;
+
+ if ((mp = allocmap()) == NULL)
+ return NULL;
+
+ *mp = *from;
+ initref(&mp->ref);
+ mapseg(mp); /* FIXME: we have to duplicate the pages */
+
+ return mp;
+}
diff --git a/src/os9/proc.c b/src/os9/proc.c
@@ -92,112 +92,6 @@ gettask(int pid)
return tp;
}
-int
-unmapseg(Map *mp)
-{
- Page *p;
-
- dbg("unmap %s: %llx -> %llx\n",
- (mp->name) ? mp->name : "unnamed",
- mp->va, mp->pa);
-
- for (p = mp->pages; p; p = p->next) {
- vunmap(p->pa, p->va);
- freep(p);
- }
-
- mp->pages = NULL;
-
- return 0;
-}
-
-int
-mapseg(Map *mp)
-{
- phyaddr_t pa;
- uintptr_t va;
- unsigned long i, n;
- Page *p;
-
- assert(mp->perm & MR);
-
- dbg("map %s: %llx -> %llx %c %c%c%c (%llu)\n",
- (mp->name) ? mp->name : "unnamed",
- mp->va, mp->pa,
- (mp->perm & MD) ? 'd' : 'n',
- (mp->perm & MR) ? 'r' : '-',
- (mp->perm & MW) ? 'w' : '-',
- (mp->perm & MX) ? 'x' : '-',
- mp->siz);
-
- n = (mp->siz + PAGESIZE-1)/PAGESIZE;
- va = mp->va;
- pa = mp->pa;
-
- for (i = 0; i < n; i++) {
- if (mp->pa == -1)
- pa = -1;
-
- if ((p = allocp(pa)) == NULL)
- goto err;
- p->va = va;
- p->next = mp->pages;
-
- if (vmap(p->pa, va, mp->perm) < 0) {
- freep(p);
- goto err;
- }
-
- mp->pages = p;
- pa += PAGESIZE;
- va += PAGESIZE;
- }
-
- return 0;
-
-err:
- unmapseg(mp);
- return -1;
-}
-
-Map *
-newstack(Task *tp)
-{
- Map *mp = &tp->stack;
-
- initref(&mp->ref);
- mp->name = "stack";
- mp->perm = MW | MR;
-
- mp->pa = -1;
- mp->va = STACKADDR & (STACKADDR-1);
- mp->siz = PAGESIZE;
-
- if (mapseg(mp) < 0)
- return NULL;
-
- return mp;
-}
-
-Map *
-newkstack(Task *tp)
-{
- Map *mp = &tp->kstack;
-
- initref(&mp->ref);
- mp->name = "kstack";
- mp->perm = MW | MR;
-
- mp->pa = -1;
- mp->va = KSTACKADDR & (STACKADDR-1);
- mp->siz = PAGESIZE;
-
- if (mapseg(mp) < 0)
- return NULL;
-
- return mp;
-}
-
static Task *
newslot(void)
{
@@ -291,21 +185,6 @@ newfds(Fdset *from)
return fds;
}
-static Map *
-newmap(Map *from)
-{
- Map *mp;
-
- if ((mp = allocmap()) == NULL)
- return NULL;
-
- *mp = *from;
- initref(&mp->ref);
- mapseg(mp); /* FIXME: we have to duplicate the pages */
-
- return mp;
-}
-
static void
delspace(Nspace *ns)
{
@@ -322,15 +201,6 @@ delfds(Fdset *fds)
freefds(fds);
}
-static void
-delmap(Map *mp)
-{
- if (!decref(&mp->ref))
- return;
- unmapseg(mp);
- freemap(mp);
-}
-
/*
* tp must be blocked when this function is called
*/