Changeset e49e234 in mainline for kernel/generic
- Timestamp:
- 2009-02-27T11:32:31Z (17 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- c1f7f6ea
- Parents:
- 5f0f29ce
- Location:
- kernel/generic
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
kernel/generic/include/align.h
r5f0f29ce re49e234 27 27 */ 28 28 29 /** @addtogroup generic 29 /** @addtogroup generic 30 30 * @ingroup others 31 31 * @{ … … 33 33 /** 34 34 * @file 35 * @brief 35 * @brief Macros for making values and addresses aligned. 36 36 */ 37 37 … … 44 44 * @param a Size of alignment, must be power of 2. 45 45 */ 46 #define ALIGN_DOWN(s, a) 46 #define ALIGN_DOWN(s, a) ((s) & ~((a) - 1)) 47 47 48 48 … … 52 52 * @param a Size of alignment, must be power of 2. 53 53 */ 54 #define ALIGN_UP(s, a) 54 #define ALIGN_UP(s, a) (((s) + ((a) - 1)) & ~((a) - 1)) 55 55 56 56 #endif -
kernel/generic/include/mm/frame.h
r5f0f29ce re49e234 40 40 #include <adt/list.h> 41 41 #include <mm/buddy.h> 42 #include <synch/spinlock.h> 42 43 #include <arch/mm/page.h> 43 44 #include <arch/mm/frame.h> … … 68 69 typedef uint8_t zone_flags_t; 69 70 71 /** Available zone (free for allocation) */ 72 #define ZONE_AVAILABLE 0x00 70 73 /** Zone is reserved (not available for allocation) */ 71 #define ZONE_RESERVED 0x0874 #define ZONE_RESERVED 0x08 72 75 /** Zone is used by firmware (not available for allocation) */ 73 #define ZONE_FIRMWARE 0x1076 #define ZONE_FIRMWARE 0x10 74 77 75 78 /** Currently there is no equivalent zone flags 76 79 for frame flags */ 77 80 #define FRAME_TO_ZONE_FLAGS(frame_flags) 0 81 82 typedef struct { 83 count_t refcount; /**< Tracking of shared frames */ 84 uint8_t buddy_order; /**< Buddy system block order */ 85 link_t buddy_link; /**< Link to the next free block inside 86 one order */ 87 void *parent; /**< If allocated by slab, this points there */ 88 } frame_t; 89 90 typedef struct { 91 pfn_t base; /**< Frame_no of the first frame 92 in the frames array */ 93 count_t count; /**< Size of zone */ 94 count_t free_count; /**< Number of free frame_t 95 structures */ 96 count_t busy_count; /**< Number of busy frame_t 97 structures */ 98 zone_flags_t flags; /**< Type of the zone */ 99 100 frame_t *frames; /**< Array of frame_t structures 101 in this zone */ 102 buddy_system_t *buddy_system; /**< Buddy system for the zone */ 103 } zone_t; 104 105 /* 106 * The zoneinfo.lock must be locked when accessing zoneinfo structure. 107 * Some of the attributes in zone_t structures are 'read-only' 108 */ 109 typedef struct { 110 SPINLOCK_DECLARE(lock); 111 count_t count; 112 zone_t info[ZONES_MAX]; 113 } zones_t; 114 115 extern zones_t zones; 78 116 79 117 static inline uintptr_t PFN2ADDR(pfn_t frame) … … 99 137 } 100 138 139 static inline bool zone_flags_available(zone_flags_t flags) 140 { 141 return ((flags & (ZONE_RESERVED | ZONE_FIRMWARE)) == 0); 142 } 143 101 144 #define IS_BUDDY_ORDER_OK(index, order) \ 102 145 ((~(((unative_t) -1) << (order)) & (index)) == 0) … … 118 161 extern void frame_reference_add(pfn_t); 119 162 163 extern count_t find_zone(pfn_t frame, count_t count, count_t hint); 120 164 extern count_t zone_create(pfn_t, count_t, pfn_t, zone_flags_t); 121 165 extern void *frame_get_parent(pfn_t, count_t); -
kernel/generic/include/mm/page.h
r5f0f29ce re49e234 62 62 63 63 extern uintptr_t hw_map(uintptr_t physaddr, size_t size); 64 extern void hw_area(void);65 64 66 65 #endif -
kernel/generic/src/ddi/ddi.c
r5f0f29ce re49e234 30 30 * @{ 31 31 */ 32 32 33 33 /** 34 34 * @file 35 * @brief 35 * @brief Device Driver Interface functions. 36 36 * 37 37 * This file contains functions that comprise the Device Driver Interface. … … 48 48 #include <synch/spinlock.h> 49 49 #include <syscall/copy.h> 50 #include <adt/ list.h>50 #include <adt/btree.h> 51 51 #include <arch.h> 52 52 #include <align.h> … … 56 56 SPINLOCK_INITIALIZE(parea_lock); 57 57 58 /** Listwith enabled physical memory areas. */59 static LIST_INITIALIZE(parea_head);58 /** B+tree with enabled physical memory areas. */ 59 static btree_t parea_btree; 60 60 61 61 /** Initialize DDI. */ 62 62 void ddi_init(void) 63 63 { 64 hw_area();64 btree_create(&parea_btree); 65 65 } 66 66 … … 69 69 * @param parea Pointer to physical area structure. 70 70 * 71 * @todo This function doesn't check for overlaps. It depends on the kernel to72 * create disjunct physical memory areas.73 71 */ 74 72 void ddi_parea_register(parea_t *parea) 75 73 { 76 ipl_t ipl; 77 78 ipl = interrupts_disable(); 74 ipl_t ipl = interrupts_disable(); 79 75 spinlock_lock(&parea_lock); 80 76 81 77 /* 82 * TODO: we should really check for overlaps here. 83 * However, we should be safe because the kernel is pretty sane. 84 */ 85 link_initialize(&parea->link); 86 list_append(&parea->link, &parea_head); 78 * We don't check for overlaps here as the kernel is pretty sane. 79 */ 80 btree_insert(&parea_btree, (btree_key_t) parea->pbase, parea, NULL); 87 81 88 82 spinlock_unlock(&parea_lock); … … 92 86 /** Map piece of physical memory into virtual address space of current task. 93 87 * 94 * @param pf Physical address of the starting frame.95 * @param vp Virtual address of the starting page.88 * @param pf Physical address of the starting frame. 89 * @param vp Virtual address of the starting page. 96 90 * @param pages Number of pages to map. 97 91 * @param flags Address space area flags for the mapping. 98 92 * 99 93 * @return 0 on success, EPERM if the caller lacks capabilities to use this 100 * syscall, ENOENT if there is no task matching the specified ID or the 101 * physical address space is not enabled for mapping and ENOMEM if there 102 * was a problem in creating address space area. 103 */ 104 static int ddi_physmem_map(uintptr_t pf, uintptr_t vp, pfn_t pages, int flags) 105 { 106 ipl_t ipl; 107 cap_t caps; 94 * syscall, EBADMEM if pf or vf is not page aligned, ENOENT if there 95 * is no task matching the specified ID or the physical address space 96 * is not enabled for mapping and ENOMEM if there was a problem in 97 * creating address space area. 98 * 99 */ 100 static int ddi_physmem_map(uintptr_t pf, uintptr_t vp, count_t pages, int flags) 101 { 102 ASSERT(TASK); 103 ASSERT((pf % FRAME_SIZE) == 0); 104 ASSERT((vp % PAGE_SIZE) == 0); 105 106 /* 107 * Make sure the caller is authorised to make this syscall. 108 */ 109 cap_t caps = cap_get(TASK); 110 if (!(caps & CAP_MEM_MANAGER)) 111 return EPERM; 112 108 113 mem_backend_data_t backend_data; 109 110 114 backend_data.base = pf; 111 115 backend_data.frames = pages; 112 116 113 /* 114 * Make sure the caller is authorised to make this syscall. 115 */ 116 caps = cap_get(TASK); 117 if (!(caps & CAP_MEM_MANAGER)) 118 return EPERM; 119 120 ipl = interrupts_disable(); 121 122 /* 123 * Check if the physical memory area is enabled for mapping. 124 */ 125 spinlock_lock(&parea_lock); 126 127 bool fnd = false; 128 link_t *cur; 129 130 for (cur = parea_head.next; cur != &parea_head; cur = cur->next) { 131 parea_t *parea = list_get_instance(cur, parea_t, link); 132 if ((parea->pbase <= pf) && (ADDR2PFN(pf - parea->pbase) + pages <= parea->frames)) { 133 fnd = true; 134 break; 135 } 136 } 137 138 spinlock_unlock(&parea_lock); 139 140 if (!fnd) { 141 /* 142 * Physical memory area cannot be mapped. 143 */ 144 interrupts_restore(ipl); 145 return ENOENT; 146 } 147 117 ipl_t ipl = interrupts_disable(); 118 119 /* Find the zone of the physical memory */ 120 spinlock_lock(&zones.lock); 121 count_t znum = find_zone(ADDR2PFN(pf), pages, 0); 122 123 if (znum == (count_t) -1) { 124 /* Frames not found in any zones 125 * -> assume it is hardware device and allow mapping 126 */ 127 spinlock_unlock(&zones.lock); 128 goto map; 129 } 130 131 if (zones.info[znum].flags & ZONE_FIRMWARE) { 132 /* Frames are part of firmware */ 133 spinlock_unlock(&zones.lock); 134 goto map; 135 } 136 137 if (zone_flags_available(zones.info[znum].flags)) { 138 /* Frames are part of physical memory, check if the memory 139 * region is enabled for mapping. 140 */ 141 spinlock_unlock(&zones.lock); 142 143 spinlock_lock(&parea_lock); 144 btree_node_t *nodep; 145 parea_t *parea = (parea_t *) btree_search(&parea_btree, 146 (btree_key_t) pf, &nodep); 147 148 if ((!parea) || (parea->frames < pages)) 149 goto err; 150 151 spinlock_unlock(&parea_lock); 152 goto map; 153 } 154 155 err: 156 spinlock_unlock(&zones.lock); 157 interrupts_restore(ipl); 158 return ENOENT; 159 160 map: 148 161 spinlock_lock(&TASK->lock); 149 162 150 if (!as_area_create(TASK->as, flags, pages * PAGE_SIZE, vp, AS_AREA_ATTR_NONE,151 163 if (!as_area_create(TASK->as, flags, pages * PAGE_SIZE, vp, 164 AS_AREA_ATTR_NONE, &phys_backend, &backend_data)) { 152 165 /* 153 166 * The address space area could not have been created. … … 175 188 * 176 189 * @return 0 on success, EPERM if the caller lacks capabilities to use this 177 * syscall, ENOENT if there is no task matching the specified ID. 190 * syscall, ENOENT if there is no task matching the specified ID. 191 * 178 192 */ 179 193 static int ddi_iospace_enable(task_id_t id, uintptr_t ioaddr, size_t size) 180 194 { 181 ipl_t ipl;182 cap_t caps;183 task_t *t;184 int rc;185 186 195 /* 187 196 * Make sure the caller is authorised to make this syscall. 188 197 */ 189 cap s = cap_get(TASK);198 cap_t caps = cap_get(TASK); 190 199 if (!(caps & CAP_IO_MANAGER)) 191 200 return EPERM; 192 201 193 ipl = interrupts_disable();202 ipl_t ipl = interrupts_disable(); 194 203 spinlock_lock(&tasks_lock); 195 204 196 t = task_find_by_id(id);197 198 if ((!t ) || (!context_check(CONTEXT, t->context))) {205 task_t *task = task_find_by_id(id); 206 207 if ((!task) || (!context_check(CONTEXT, task->context))) { 199 208 /* 200 209 * There is no task with the specified ID … … 206 215 return ENOENT; 207 216 } 208 217 209 218 /* Lock the task and release the lock protecting tasks_btree. */ 210 spinlock_lock(&t ->lock);219 spinlock_lock(&task->lock); 211 220 spinlock_unlock(&tasks_lock); 212 213 rc = ddi_iospace_enable_arch(t, ioaddr, size); 214 215 spinlock_unlock(&t->lock); 216 interrupts_restore(ipl); 221 222 int rc = ddi_iospace_enable_arch(task, ioaddr, size); 223 224 spinlock_unlock(&task->lock); 225 interrupts_restore(ipl); 226 217 227 return rc; 218 228 } … … 226 236 * 227 237 * @return 0 on success, otherwise it returns error code found in errno.h 228 */ 238 * 239 */ 229 240 unative_t sys_physmem_map(unative_t phys_base, unative_t virt_base, 230 241 unative_t pages, unative_t flags) … … 232 243 return (unative_t) ddi_physmem_map(ALIGN_DOWN((uintptr_t) phys_base, 233 244 FRAME_SIZE), ALIGN_DOWN((uintptr_t) virt_base, PAGE_SIZE), 234 ( pfn_t) pages, (int) flags);245 (count_t) pages, (int) flags); 235 246 } 236 247 … … 240 251 * 241 252 * @return 0 on success, otherwise it returns error code found in errno.h 242 */ 253 * 254 */ 243 255 unative_t sys_iospace_enable(ddi_ioarg_t *uspace_io_arg) 244 256 { 245 257 ddi_ioarg_t arg; 246 int rc; 247 248 rc = copy_from_uspace(&arg, uspace_io_arg, sizeof(ddi_ioarg_t)); 258 int rc = copy_from_uspace(&arg, uspace_io_arg, sizeof(ddi_ioarg_t)); 249 259 if (rc != 0) 250 260 return (unative_t) rc; 251 261 252 262 return (unative_t) ddi_iospace_enable((task_id_t) arg.task_id, 253 263 (uintptr_t) arg.ioaddr, (size_t) arg.size); … … 257 267 * 258 268 * @param enable If non-zero, the preemption counter will be decremented, 259 * leading to potential enabling of preemption. Otherwise the preemption 260 * counter will be incremented, preventing preemption from occurring. 269 * leading to potential enabling of preemption. Otherwise 270 * the preemption counter will be incremented, preventing 271 * preemption from occurring. 261 272 * 262 273 * @return Zero on success or EPERM if callers capabilities are not sufficient. 263 */ 274 * 275 */ 264 276 unative_t sys_preempt_control(int enable) 265 277 { 266 278 if (!cap_get(TASK) & CAP_PREEMPT_CONTROL) 267 279 return EPERM; 280 268 281 if (enable) 269 282 preemption_enable(); 270 283 else 271 284 preemption_disable(); 285 272 286 return 0; 273 287 } -
kernel/generic/src/mm/frame.c
r5f0f29ce re49e234 49 49 #include <debug.h> 50 50 #include <adt/list.h> 51 #include <synch/spinlock.h>52 51 #include <synch/mutex.h> 53 52 #include <synch/condvar.h> … … 61 60 #include <config.h> 62 61 63 typedef struct { 64 count_t refcount; /**< Tracking of shared frames */ 65 uint8_t buddy_order; /**< Buddy system block order */ 66 link_t buddy_link; /**< Link to the next free block inside 67 one order */ 68 void *parent; /**< If allocated by slab, this points there */ 69 } frame_t; 70 71 typedef struct { 72 pfn_t base; /**< Frame_no of the first frame 73 in the frames array */ 74 count_t count; /**< Size of zone */ 75 count_t free_count; /**< Number of free frame_t 76 structures */ 77 count_t busy_count; /**< Number of busy frame_t 78 structures */ 79 zone_flags_t flags; /**< Type of the zone */ 80 81 frame_t *frames; /**< Array of frame_t structures 82 in this zone */ 83 buddy_system_t *buddy_system; /**< Buddy system for the zone */ 84 } zone_t; 85 86 /* 87 * The zoneinfo.lock must be locked when accessing zoneinfo structure. 88 * Some of the attributes in zone_t structures are 'read-only' 89 */ 90 typedef struct { 91 SPINLOCK_DECLARE(lock); 92 count_t count; 93 zone_t info[ZONES_MAX]; 94 } zones_t; 95 96 static zones_t zones; 62 zones_t zones; 97 63 98 64 /* … … 127 93 { 128 94 return (frame - zone->frames); 129 }130 131 static inline bool zone_flags_available(zone_flags_t flags)132 {133 return ((flags & (ZONE_RESERVED | ZONE_FIRMWARE)) == 0);134 95 } 135 96 … … 181 142 /* Move other zones up */ 182 143 count_t j; 183 for (j = i; j < zones.count; j++) 184 zones.info[j + 1] = zones.info[j]; 144 for (j = zones.count; j > i; j--) { 145 zones.info[j] = zones.info[j - 1]; 146 zones.info[j].buddy_system->data = 147 (void *) &zones.info[j - 1]; 148 } 185 149 186 150 zones.count++; … … 207 171 } 208 172 209 /** Find a zone with a given frame .173 /** Find a zone with a given frames. 210 174 * 211 175 * Assume interrupts are disabled and zones lock is … … 213 177 * 214 178 * @param frame Frame number contained in zone. 179 * @param count Number of frames to look for. 215 180 * @param hint Used as zone hint. 216 181 * … … 218 183 * 219 184 */ 220 static count_t find_zone(pfn_t frame, count_t hint)185 count_t find_zone(pfn_t frame, count_t count, count_t hint) 221 186 { 222 187 if (hint >= zones.count) … … 226 191 do { 227 192 if ((zones.info[i].base <= frame) 228 && (zones.info[i].base + zones.info[i].count > frame))193 && (zones.info[i].base + zones.info[i].count >= frame + count)) 229 194 return i; 230 195 … … 766 731 zones.info[z2].count); 767 732 768 /* Shift existing zones*/733 /* Move zones down */ 769 734 count_t i; 770 for (i = z2 + 1; i < zones.count; i++) 735 for (i = z2 + 1; i < zones.count; i++) { 771 736 zones.info[i - 1] = zones.info[i]; 737 zones.info[i - 1].buddy_system->data = 738 (void *) &zones.info[i - 1]; 739 } 740 772 741 zones.count--; 773 742 … … 965 934 spinlock_lock(&zones.lock); 966 935 967 count_t znum = find_zone(pfn, hint);936 count_t znum = find_zone(pfn, 1, hint); 968 937 969 938 ASSERT(znum != (count_t) -1); … … 981 950 spinlock_lock(&zones.lock); 982 951 983 count_t znum = find_zone(pfn, hint);952 count_t znum = find_zone(pfn, 1, hint); 984 953 985 954 ASSERT(znum != (count_t) -1); … … 1112 1081 */ 1113 1082 pfn_t pfn = ADDR2PFN(frame); 1114 count_t znum = find_zone(pfn, NULL);1083 count_t znum = find_zone(pfn, 1, NULL); 1115 1084 1116 1085 ASSERT(znum != (count_t) -1); … … 1151 1120 * First, find host frame zone for addr. 1152 1121 */ 1153 count_t znum = find_zone(pfn, NULL);1122 count_t znum = find_zone(pfn, 1, NULL); 1154 1123 1155 1124 ASSERT(znum != (count_t) -1); … … 1169 1138 count_t i; 1170 1139 for (i = 0; i < count; i++) { 1171 count_t znum = find_zone(start + i, 0);1140 count_t znum = find_zone(start + i, 1, 0); 1172 1141 if (znum == (count_t) -1) /* PFN not found */ 1173 1142 continue; … … 1238 1207 { 1239 1208 #ifdef __32_BITS__ 1240 printf("# base address f lags free frames busy frames\n");1241 printf("-- ------------ -------- ------------ ------------\n");1209 printf("# base address frames flags free frames busy frames\n"); 1210 printf("-- ------------ ------------ -------- ------------ ------------\n"); 1242 1211 #endif 1243 1212 1244 1213 #ifdef __64_BITS__ 1245 printf("# base address flags free frames busy frames\n");1246 printf("-- -------------------- -------- ------------ ------------\n");1214 printf("# base address frames flags free frames busy frames\n"); 1215 printf("-- -------------------- ------------ -------- ------------ ------------\n"); 1247 1216 #endif 1248 1217 … … 1270 1239 1271 1240 uintptr_t base = PFN2ADDR(zones.info[i].base); 1241 count_t count = zones.info[i].count; 1272 1242 zone_flags_t flags = zones.info[i].flags; 1273 1243 count_t free_count = zones.info[i].free_count; … … 1279 1249 bool available = zone_flags_available(flags); 1280 1250 1251 printf("%-2" PRIc, i); 1252 1281 1253 #ifdef __32_BITS__ 1282 printf("%-2" PRIc " %10p %c%c%c ", i, base, 1254 printf(" %10p", base); 1255 #endif 1256 1257 #ifdef __64_BITS__ 1258 printf(" %18p", base); 1259 #endif 1260 1261 printf(" %12" PRIc " %c%c%c ", count, 1283 1262 available ? 'A' : ' ', 1284 1263 (flags & ZONE_RESERVED) ? 'R' : ' ', 1285 1264 (flags & ZONE_FIRMWARE) ? 'F' : ' '); 1286 #endif1287 1288 #ifdef __64_BITS__1289 printf("%-2" PRIc " %18p %c%c%c ", i, base,1290 available ? 'A' : ' ',1291 (flags & ZONE_RESERVED) ? 'R' : ' ',1292 (flags & ZONE_FIRMWARE) ? 'F' : ' ');1293 #endif1294 1265 1295 1266 if (available) 1296 1267 printf("%12" PRIc " %12" PRIc, 1297 1268 free_count, busy_count); 1269 1298 1270 printf("\n"); 1299 1271 }
Note:
See TracChangeset
for help on using the changeset viewer.