vm: merge i386 and arm pagetable code
[minix.git] / servers / vm / pb.c
blob736c7f9861f8a035da5a3bfdf1b55d2e8708af00
2 #define _SYSTEM 1
4 #include <minix/com.h>
5 #include <minix/callnr.h>
6 #include <minix/type.h>
7 #include <minix/config.h>
8 #include <minix/const.h>
9 #include <minix/sysutil.h>
10 #include <minix/syslib.h>
11 #include <minix/debug.h>
12 #include <minix/bitmap.h>
13 #include <minix/hash.h>
15 #include <sys/mman.h>
17 #include <limits.h>
18 #include <string.h>
19 #include <errno.h>
20 #include <assert.h>
21 #include <stdint.h>
22 #include <sys/param.h>
24 #include "vm.h"
25 #include "proto.h"
26 #include "util.h"
27 #include "glo.h"
28 #include "region.h"
29 #include "sanitycheck.h"
30 #include "physravl.h"
31 #include "memlist.h"
33 struct phys_block *pb_new(phys_bytes phys)
35 struct phys_block *newpb;
37 if(!SLABALLOC(newpb)) {
38 printf("vm: pb_new: couldn't allocate phys block\n");
39 return NULL;
42 if(phys != MAP_NONE)
43 assert(!(phys % VM_PAGE_SIZE));
45 USE(newpb,
46 newpb->phys = phys;
47 newpb->refcount = 0;
48 newpb->firstregion = NULL;
51 return newpb;
54 void pb_free(struct phys_block *pb)
56 if(pb->phys != MAP_NONE)
57 free_mem(ABS2CLICK(pb->phys), 1);
58 SLABFREE(pb);
61 void pb_link(struct phys_region *newphysr, struct phys_block *newpb,
62 vir_bytes offset, struct vir_region *parent)
64 USE(newphysr,
65 newphysr->offset = offset;
66 newphysr->ph = newpb;
67 newphysr->parent = parent;
68 newphysr->next_ph_list = newpb->firstregion;
69 newphysr->memtype = parent->memtype;
70 newpb->firstregion = newphysr;);
71 newpb->refcount++;
74 struct phys_region *pb_reference(struct phys_block *newpb,
75 vir_bytes offset, struct vir_region *region)
77 struct phys_region *newphysr;
79 if(!SLABALLOC(newphysr)) {
80 printf("vm: pb_reference: couldn't allocate phys region\n");
81 return NULL;
84 /* New physical region. */
85 pb_link(newphysr, newpb, offset, region);
87 physr_insert(region->phys, newphysr);
89 return newphysr;
92 /*===========================================================================*
93 * pb_unreferenced *
94 *===========================================================================*/
95 void pb_unreferenced(struct vir_region *region, struct phys_region *pr, int rm)
97 struct phys_block *pb;
99 pb = pr->ph;
100 assert(pb->refcount > 0);
101 USE(pb, pb->refcount--;);
102 assert(pb->refcount >= 0);
104 if(pb->firstregion == pr) {
105 USE(pb, pb->firstregion = pr->next_ph_list;);
106 } else {
107 struct phys_region *others;
109 for(others = pb->firstregion; others;
110 others = others->next_ph_list) {
111 assert(others->ph == pb);
112 if(others->next_ph_list == pr) {
113 USE(others, others->next_ph_list = pr->next_ph_list;);
114 break;
118 assert(others); /* Otherwise, wasn't on the list. */
121 if(pb->refcount == 0) {
122 assert(!pb->firstregion);
123 int r;
124 if((r = region->memtype->ev_unreference(pr)) != OK)
125 panic("unref failed, %d", r);
127 SLABFREE(pb);
130 pr->ph = NULL;
132 if(rm) physr_remove(region->phys, pr->offset);