3 * \brief Kernel capability management implementation.
7 * Copyright (c) 2007-2012,2015,2016 ETH Zurich.
8 * Copyright (c) 2015, 2016 Hewlett Packard Enterprise Development LP.
11 * This file is distributed under the terms in the attached LICENSE file.
12 * If you do not find this file, copies can be found by writing to:
13 * ETH Zurich D-INFK, Universitaetstr. 6, CH-8092 Zurich. Attn: Systems Group.
19 #include <barrelfish_kpi/syscalls.h>
20 #include <barrelfish_kpi/paging_arch.h>
21 #include <barrelfish_kpi/lmp.h>
23 #include <capabilities.h>
24 #include <cap_predicates.h>
28 #include <paging_kernel_arch.h>
30 #include <mdb/mdb_tree.h>
31 #include <trace/trace.h>
32 #include <trace_definitions/trace_defs.h>
34 #include <bitmacros.h>
37 #pragma GCC diagnostic ignored "-Wsuggest-attribute=noreturn"
39 #ifdef TRACE_PMEM_CAPS
40 uint64_t trace_types_enabled = TRACE_TYPES_ENABLED_INITIAL;
41 genpaddr_t TRACE_PMEM_BEGIN = TRACE_PMEM_BEGIN_INITIAL;
42 gensize_t TRACE_PMEM_SIZE = TRACE_PMEM_SIZE_INITIAL;
44 void caps_trace_ctrl(uint64_t types, genpaddr_t start, gensize_t size)
47 trace_types_enabled = types;
48 TRACE_PMEM_BEGIN = start;
49 TRACE_PMEM_SIZE = size;
51 trace_types_enabled = 0;
56 struct capability monitor_ep;
58 STATIC_ASSERT(48 == ObjType_Num, "Knowledge of all cap types");
59 int sprint_cap(char *buf, size_t len, struct capability *cap)
62 case ObjType_PhysAddr:
63 return snprintf(buf, len,
64 "physical address range cap (0x%" PRIxGENPADDR ":0x%" PRIxGENSIZE ")",
65 cap->u.physaddr.base, cap->u.physaddr.bytes);
68 return snprintf(buf, len, "RAM cap (0x%" PRIxGENPADDR ":0x%" PRIxGENSIZE ")",
69 cap->u.ram.base, cap->u.ram.bytes);
71 case ObjType_L1CNode: {
72 int ret = snprintf(buf, len, "L1 CNode cap "
73 "(base=%#"PRIxGENPADDR", allocated bytes %#"PRIxGENSIZE
74 ", rights mask %#"PRIxCAPRIGHTS")",
75 get_address(cap), get_size(cap),
76 cap->u.l1cnode.rightsmask);
80 case ObjType_L2CNode: {
81 int ret = snprintf(buf, len, "L2 CNode cap "
82 "(base=%#"PRIxGENPADDR", rights mask %#"PRIxCAPRIGHTS")",
83 get_address(cap), cap->u.l1cnode.rightsmask);
87 case ObjType_Dispatcher:
88 return snprintf(buf, len, "Dispatcher cap %p", cap->u.dispatcher.dcb);
91 return snprintf(buf, len, "Frame cap (0x%" PRIxGENPADDR ":0x%" PRIxGENSIZE ")",
92 cap->u.frame.base, cap->u.frame.bytes);
94 case ObjType_DevFrame:
95 return snprintf(buf, len, "Device Frame cap (0x%" PRIxGENPADDR ":0x%" PRIxGENSIZE ")",
96 cap->u.frame.base, cap->u.devframe.bytes);
98 case ObjType_VNode_ARM_l1:
99 return snprintf(buf, len, "ARM L1 table at 0x%" PRIxGENPADDR,
100 cap->u.vnode_arm_l1.base);
102 case ObjType_VNode_ARM_l2:
103 return snprintf(buf, len, "ARM L2 table at 0x%" PRIxGENPADDR,
104 cap->u.vnode_arm_l2.base);
106 case ObjType_VNode_AARCH64_l0:
107 return snprintf(buf, len, "AARCH64 L0 table at 0x%" PRIxGENPADDR,
108 cap->u.vnode_aarch64_l0.base);
110 case ObjType_VNode_AARCH64_l1:
111 return snprintf(buf, len, "AARCH64 L1 table at 0x%" PRIxGENPADDR,
112 cap->u.vnode_aarch64_l1.base);
114 case ObjType_VNode_AARCH64_l2:
115 return snprintf(buf, len, "AARCH64 L2 table at 0x%" PRIxGENPADDR,
116 cap->u.vnode_aarch64_l2.base);
118 case ObjType_VNode_AARCH64_l3:
119 return snprintf(buf, len, "AARCH64 L3 table at 0x%" PRIxGENPADDR,
120 cap->u.vnode_aarch64_l3.base);
122 case ObjType_VNode_x86_32_ptable:
123 return snprintf(buf, len, "x86_32 Page table at 0x%" PRIxGENPADDR,
124 cap->u.vnode_x86_32_ptable.base);
126 case ObjType_VNode_x86_32_pdir:
127 return snprintf(buf, len, "x86_32 Page directory at 0x%" PRIxGENPADDR,
128 cap->u.vnode_x86_32_pdir.base);
130 case ObjType_VNode_x86_32_pdpt:
131 return snprintf(buf, len, "x86_32 PDPT at 0x%" PRIxGENPADDR,
132 cap->u.vnode_x86_32_pdpt.base);
134 case ObjType_VNode_x86_64_ptable:
135 return snprintf(buf, len, "x86_64 Page table at 0x%" PRIxGENPADDR,
136 cap->u.vnode_x86_64_ptable.base);
138 case ObjType_VNode_x86_64_pdir:
139 return snprintf(buf, len, "x86_64 Page directory at 0x%" PRIxGENPADDR,
140 cap->u.vnode_x86_64_pdir.base);
142 case ObjType_VNode_x86_64_pdpt:
143 return snprintf(buf, len, "x86_64 PDPT at 0x%" PRIxGENPADDR,
144 cap->u.vnode_x86_64_pdpt.base);
146 case ObjType_VNode_x86_64_pml4:
147 return snprintf(buf, len, "x86_64 PML4 at 0x%" PRIxGENPADDR,
148 cap->u.vnode_x86_64_pml4.base);
150 case ObjType_Frame_Mapping:
151 return snprintf(buf, len, "Frame Mapping (Frame cap @%p, "
152 "pte @0x%"PRIxLVADDR", pte_count=%hu)",
153 cap->u.frame_mapping.cap,
154 cap->u.frame_mapping.pte,
155 cap->u.frame_mapping.pte_count);
157 case ObjType_DevFrame_Mapping:
158 return snprintf(buf, len, "DevFrame Mapping (DevFrame cap @%p, "
159 "pte @0x%"PRIxLVADDR", pte_count=%hu)",
160 cap->u.devframe_mapping.cap,
161 cap->u.devframe_mapping.pte,
162 cap->u.devframe_mapping.pte_count);
164 case ObjType_VNode_x86_64_pml4_Mapping:
165 return snprintf(buf, len, "x86_64 PML4 Mapping (x86_64 PML4 cap @%p, "
166 "pte @0x%"PRIxLVADDR", pte_count=%hu)",
167 cap->u.vnode_x86_64_pml4_mapping.cap,
168 cap->u.vnode_x86_64_pml4_mapping.pte,
169 cap->u.vnode_x86_64_pml4_mapping.pte_count);
171 case ObjType_VNode_x86_64_pdpt_Mapping:
172 return snprintf(buf, len, "x86_64 PDPT Mapping (x86_64 PDPT cap @%p, "
173 "pte @0x%"PRIxLVADDR", pte_count=%hu)",
174 cap->u.vnode_x86_64_pdpt_mapping.cap,
175 cap->u.vnode_x86_64_pdpt_mapping.pte,
176 cap->u.vnode_x86_64_pdpt_mapping.pte_count);
178 case ObjType_VNode_x86_64_pdir_Mapping:
179 return snprintf(buf, len, "x86_64 PDIR Mapping (x86_64 PDIR cap @%p, "
180 "pte @0x%"PRIxLVADDR", pte_count=%hu)",
181 cap->u.vnode_x86_64_pdir_mapping.cap,
182 cap->u.vnode_x86_64_pdir_mapping.pte,
183 cap->u.vnode_x86_64_pdir_mapping.pte_count);
185 case ObjType_VNode_x86_64_ptable_Mapping:
186 return snprintf(buf, len, "x86_64 PTABLE Mapping (x86_64 PTABLE cap @%p, "
187 "pte @0x%"PRIxLVADDR", pte_count=%hu)",
188 cap->u.vnode_x86_64_ptable_mapping.cap,
189 cap->u.vnode_x86_64_ptable_mapping.pte,
190 cap->u.vnode_x86_64_ptable_mapping.pte_count);
192 case ObjType_VNode_x86_32_pdpt_Mapping:
193 return snprintf(buf, len, "x86_32 PDPT Mapping (x86_32 PDPT cap @%p, "
194 "pte @0x%"PRIxLVADDR", pte_count=%hu)",
195 cap->u.vnode_x86_32_pdpt_mapping.cap,
196 cap->u.vnode_x86_32_pdpt_mapping.pte,
197 cap->u.vnode_x86_32_pdpt_mapping.pte_count);
199 case ObjType_VNode_x86_32_pdir_Mapping:
200 return snprintf(buf, len, "x86_32 PDIR Mapping (x86_32 PDIR cap @%p, "
201 "pte @0x%"PRIxLVADDR", pte_count=%hu)",
202 cap->u.vnode_x86_32_pdir_mapping.cap,
203 cap->u.vnode_x86_32_pdir_mapping.pte,
204 cap->u.vnode_x86_32_pdir_mapping.pte_count);
206 case ObjType_VNode_x86_32_ptable_Mapping:
207 return snprintf(buf, len, "x86_32 PTABLE Mapping (x86_32 PTABLE cap @%p, "
208 "pte @0x%"PRIxLVADDR", pte_count=%hu)",
209 cap->u.vnode_x86_32_ptable_mapping.cap,
210 cap->u.vnode_x86_32_ptable_mapping.pte,
211 cap->u.vnode_x86_32_ptable_mapping.pte_count);
213 case ObjType_VNode_ARM_l1_Mapping:
214 return snprintf(buf, len, "ARM l1 Mapping (ARM l1 cap @%p, "
215 "pte @0x%"PRIxLVADDR", pte_count=%hu)",
216 cap->u.vnode_arm_l1_mapping.cap,
217 cap->u.vnode_arm_l1_mapping.pte,
218 cap->u.vnode_arm_l1_mapping.pte_count);
220 case ObjType_VNode_ARM_l2_Mapping:
221 return snprintf(buf, len, "ARM l2 Mapping (ARM l2 cap @%p, "
222 "pte @0x%"PRIxLVADDR", pte_count=%hu)",
223 cap->u.vnode_arm_l2_mapping.cap,
224 cap->u.vnode_arm_l2_mapping.pte,
225 cap->u.vnode_arm_l2_mapping.pte_count);
227 case ObjType_VNode_AARCH64_l0_Mapping:
228 return snprintf(buf, len, "AARCH64 l0 Mapping (AARCH64 l0 cap @%p, "
229 "pte @0x%"PRIxLVADDR", pte_count=%hu)",
230 cap->u.vnode_aarch64_l0_mapping.cap,
231 cap->u.vnode_aarch64_l0_mapping.pte,
232 cap->u.vnode_aarch64_l0_mapping.pte_count);
234 case ObjType_VNode_AARCH64_l1_Mapping:
235 return snprintf(buf, len, "AARCH64 l1 Mapping (AARCH64 l1 cap @%p, "
236 "pte @0x%"PRIxLVADDR", pte_count=%hu)",
237 cap->u.vnode_aarch64_l1_mapping.cap,
238 cap->u.vnode_aarch64_l1_mapping.pte,
239 cap->u.vnode_aarch64_l1_mapping.pte_count);
241 case ObjType_VNode_AARCH64_l2_Mapping:
242 return snprintf(buf, len, "AARCH64 l2 Mapping (AARCH64 l2 cap @%p, "
243 "pte @0x%"PRIxLVADDR", pte_count=%hu)",
244 cap->u.vnode_aarch64_l2_mapping.cap,
245 cap->u.vnode_aarch64_l2_mapping.pte,
246 cap->u.vnode_aarch64_l2_mapping.pte_count);
248 case ObjType_VNode_AARCH64_l3_Mapping:
249 return snprintf(buf, len, "AARCH64 l3 Mapping (AARCH64 l3 cap @%p, "
250 "pte @0x%"PRIxLVADDR", pte_count=%hu)",
251 cap->u.vnode_aarch64_l3_mapping.cap,
252 cap->u.vnode_aarch64_l3_mapping.pte,
253 cap->u.vnode_aarch64_l3_mapping.pte_count);
255 case ObjType_IRQTable:
256 return snprintf(buf, len, "IRQTable cap");
258 case ObjType_IRQDest:
259 return snprintf(buf, len, "IRQDest cap (vec: %"PRIu64", cpu: %"PRIu64")",
260 cap->u.irqdest.vector, cap->u.irqdest.cpu);
262 case ObjType_EndPoint:
263 return snprintf(buf, len, "EndPoint cap (disp %p offset 0x%" PRIxLVADDR ")",
264 cap->u.endpoint.listener, cap->u.endpoint.epoffset);
267 return snprintf(buf, len, "IO cap (0x%hx-0x%hx)",
268 cap->u.io.start, cap->u.io.end);
271 return snprintf(buf, len, "Kernel cap");
273 case ObjType_KernelControlBlock:
274 return snprintf(buf, len, "Kernel control block");
277 return snprintf(buf, len, "ID capability (coreid 0x%" PRIxCOREID
278 " core_local_id 0x%" PRIx32 ")", cap->u.id.coreid,
279 cap->u.id.core_local_id);
281 case ObjType_PerfMon:
282 return snprintf(buf, len, "PerfMon cap");
285 return snprintf(buf, len, "Null capability (empty slot)");
288 return snprintf(buf, len, "IPI cap");
291 return snprintf(buf, len, "UNKNOWN TYPE! (%d)", cap->type);
295 void caps_trace(const char *func, int line, struct cte *cte, const char *msg)
298 sprint_cap(cap_buf, 512, &cte->cap);
302 dispatcher_handle_t handle = dcb_current->disp;
303 struct dispatcher_shared_generic *disp =
304 get_dispatcher_shared_generic(handle);
305 snprintf(disp_buf, 64, "from %.*s", DISP_NAME_LEN, disp->name);
308 strcpy(disp_buf, "no disp");
311 printk(LOG_WARN, "%s: %s:%d: %s %p %s"
312 " (owner:%" PRIuCOREID ", rc:%d/ra:%d/rd:%d)\n",
313 disp_buf, func, line, (msg ? : ""), cte, cap_buf, cte->mdbnode.owner,
314 cte->mdbnode.remote_copies, cte->mdbnode.remote_ancs,
315 cte->mdbnode.remote_descs);
319 * ID capability core_local_id counter.
321 static uint32_t id_cap_counter = 1;
324 * Sets #dest equal to #src
326 * #dest cannot be in use.
328 static errval_t set_cap(struct capability *dest, struct capability *src)
330 /* Parameter checking */
332 assert(dest != NULL);
334 debug(SUBSYS_CAPS, "Copying cap from %#"PRIxLPADDR" to %#"PRIxLPADDR"\n",
335 mem_to_local_phys((lvaddr_t)cte_for_cap(src)),
336 mem_to_local_phys((lvaddr_t)cte_for_cap(dest)));
338 // Reserved object bits must always be greater/equal to actual object size
339 assert((1UL << OBJBITS_CTE) >= sizeof(struct cte));
341 // Cannot overwrite an already existing cap
342 if (dest->type != ObjType_Null) {
343 return SYS_ERR_SLOT_IN_USE;
346 memcpy(dest, src, sizeof(struct capability));
351 * \brief Determine how many objects can be created in a specified region.
353 * This function computes the number of objects that can be created by a call
356 * \param type Type of objects to create.
357 * \param srcsize Size of memory area in bytes
358 * \param objsize For variable-sized objects, size multiplier
360 * \return Number of objects to be created, or zero on error
363 // If you create more capability types you need to deal with them
364 // in the table below.
365 STATIC_ASSERT(48 == ObjType_Num, "Knowledge of all cap types");
366 static size_t caps_max_numobjs(enum objtype type, gensize_t srcsize, gensize_t objsize)
369 case ObjType_PhysAddr:
372 case ObjType_DevFrame:
373 if (objsize > srcsize) {
376 return srcsize / objsize;
379 case ObjType_L1CNode:
380 if (srcsize < OBJSIZE_L2CNODE || objsize < OBJSIZE_L2CNODE) {
381 // disallow L1 CNode to be smaller than 16kB.
384 return srcsize / objsize;
387 case ObjType_L2CNode:
388 if (srcsize < OBJSIZE_L2CNODE || objsize != OBJSIZE_L2CNODE) {
389 // disallow L2 CNode creation if source too small or objsize wrong
392 return srcsize / objsize;
396 case ObjType_VNode_x86_64_pml4:
397 case ObjType_VNode_x86_64_pdpt:
398 case ObjType_VNode_x86_64_pdir:
399 case ObjType_VNode_x86_64_ptable:
400 case ObjType_VNode_x86_32_pdpt:
401 case ObjType_VNode_x86_32_pdir:
402 case ObjType_VNode_x86_32_ptable:
403 case ObjType_VNode_ARM_l1:
404 case ObjType_VNode_ARM_l2:
405 case ObjType_VNode_AARCH64_l0:
406 case ObjType_VNode_AARCH64_l1:
407 case ObjType_VNode_AARCH64_l2:
408 case ObjType_VNode_AARCH64_l3:
410 if (srcsize < vnode_objsize(type)) {
413 return srcsize / vnode_objsize(type);
417 case ObjType_Dispatcher:
418 if (srcsize < OBJSIZE_DISPATCHER) {
421 return srcsize / OBJSIZE_DISPATCHER;
424 case ObjType_KernelControlBlock:
425 if (srcsize < OBJSIZE_KCB) {
428 return srcsize / OBJSIZE_KCB;
432 case ObjType_IRQTable:
433 case ObjType_IRQDest:
436 case ObjType_EndPoint:
438 case ObjType_Notify_IPI:
439 case ObjType_PerfMon:
441 case ObjType_VNode_ARM_l1_Mapping:
442 case ObjType_VNode_ARM_l2_Mapping:
443 case ObjType_VNode_AARCH64_l0_Mapping:
444 case ObjType_VNode_AARCH64_l1_Mapping:
445 case ObjType_VNode_AARCH64_l2_Mapping:
446 case ObjType_VNode_AARCH64_l3_Mapping:
447 case ObjType_VNode_x86_64_pml4_Mapping:
448 case ObjType_VNode_x86_64_pdpt_Mapping:
449 case ObjType_VNode_x86_64_pdir_Mapping:
450 case ObjType_VNode_x86_64_ptable_Mapping:
451 case ObjType_VNode_x86_32_pdpt_Mapping:
452 case ObjType_VNode_x86_32_pdir_Mapping:
453 case ObjType_VNode_x86_32_ptable_Mapping:
454 case ObjType_DevFrame_Mapping:
455 case ObjType_Frame_Mapping:
459 panic("invalid type");
465 * \brief Initialize the objects for which local caps are about to be created.
467 * For the meaning of the parameters, see the 'caps_create' function.
469 STATIC_ASSERT(48 == ObjType_Num, "Knowledge of all cap types");
471 static errval_t caps_zero_objects(enum objtype type, lpaddr_t lpaddr,
472 gensize_t objsize, size_t count)
474 assert(type < ObjType_Num);
476 // Virtual address of the memory the kernel object resides in
477 // XXX: A better of doing this,
478 // this is creating caps that the kernel cannot address.
479 // It assumes that the cap is not of the type which will have to zeroed out.
481 if(lpaddr < PADDR_SPACE_LIMIT) {
482 lvaddr = local_phys_to_mem(lpaddr);
490 debug(SUBSYS_CAPS, "Frame: zeroing %zu bytes @%#"PRIxLPADDR"\n",
491 (size_t)objsize * count, lpaddr);
492 TRACE(KERNEL, BZERO, 1);
493 memset((void*)lvaddr, 0, objsize * count);
494 TRACE(KERNEL, BZERO, 0);
497 case ObjType_L1CNode:
498 case ObjType_L2CNode:
499 debug(SUBSYS_CAPS, "L%dCNode: zeroing %zu bytes @%#"PRIxLPADDR"\n",
500 type == ObjType_L1CNode ? 1 : 2, (size_t)objsize * count,
502 TRACE(KERNEL, BZERO, 1);
503 memset((void*)lvaddr, 0, objsize * count);
504 TRACE(KERNEL, BZERO, 0);
507 case ObjType_VNode_ARM_l1:
508 case ObjType_VNode_ARM_l2:
509 case ObjType_VNode_AARCH64_l0:
510 case ObjType_VNode_AARCH64_l1:
511 case ObjType_VNode_AARCH64_l2:
512 case ObjType_VNode_AARCH64_l3:
513 case ObjType_VNode_x86_32_ptable:
514 case ObjType_VNode_x86_32_pdir:
515 case ObjType_VNode_x86_32_pdpt:
516 case ObjType_VNode_x86_64_ptable:
517 case ObjType_VNode_x86_64_pdir:
518 case ObjType_VNode_x86_64_pdpt:
519 case ObjType_VNode_x86_64_pml4:
520 // objsize is size of VNode; but not given as such
521 objsize = vnode_objsize(type);
522 debug(SUBSYS_CAPS, "VNode: zeroing %zu bytes @%#"PRIxLPADDR"\n",
523 (size_t)objsize * count, lpaddr);
524 TRACE(KERNEL, BZERO, 1);
525 memset((void*)lvaddr, 0, objsize * count);
526 TRACE(KERNEL, BZERO, 0);
529 case ObjType_Dispatcher:
530 debug(SUBSYS_CAPS, "Dispatcher: zeroing %zu bytes @%#"PRIxLPADDR"\n",
531 ((size_t) OBJSIZE_DISPATCHER) * count, lpaddr);
532 TRACE(KERNEL, BZERO, 1);
533 memset((void*)lvaddr, 0, OBJSIZE_DISPATCHER * count);
534 TRACE(KERNEL, BZERO, 0);
537 case ObjType_KernelControlBlock:
538 debug(SUBSYS_CAPS, "KCB: zeroing %zu bytes @%#"PRIxLPADDR"\n",
539 ((size_t) OBJSIZE_KCB) * count, lpaddr);
540 TRACE(KERNEL, BZERO, 1);
541 memset((void*)lvaddr, 0, OBJSIZE_KCB * count);
542 TRACE(KERNEL, BZERO, 0);
546 debug(SUBSYS_CAPS, "Not zeroing %zu bytes @%#"PRIxLPADDR" for type %d\n",
547 (size_t)objsize * count, lpaddr, (int)type);
556 * \brief Create capabilities to kernel objects.
558 * This function creates 'count' kernel objects of 'type' into the memory
559 * area, based at 'addr' and of size 'objsize'. For each created kernel
560 * object, a capability is created to it and put consecutively into the array
561 * of CTEs pointed to by 'caps'. The array needs to have the appropriate size
562 * to hold all created caps. Some kernel objects can have a variable size. In
563 * that case, 'objsize' should be non-zero. and give the size multiplier. *
565 * \param type Type of objects to create.
566 * \param lpaddr Base address in the local address space.
567 * \param size Size of memory area as bytes.
568 * \param objsize For variable-sized objects, size in bytes.
569 * \param count Number of objects to be created
570 * (count <= caps_max_numobjs(type, size, objsize))
571 * \param dest_caps Pointer to array of CTEs to hold created caps.
575 // If you create more capability types you need to deal with them
576 // in the table below.
577 STATIC_ASSERT(48 == ObjType_Num, "Knowledge of all cap types");
579 static errval_t caps_create(enum objtype type, lpaddr_t lpaddr, gensize_t size,
580 gensize_t objsize, size_t count, coreid_t owner,
581 struct cte *dest_caps)
585 /* Parameter checking */
586 assert(dest_caps != NULL);
587 assert(type != ObjType_Null);
588 assert(type < ObjType_Num);
590 // objsize is 0 for non-sized types (e.g. VNodes)
591 // TODO cleanup semantics for type == CNode
592 //assert(objsize % BASE_PAGE_SIZE == 0);
593 assert(!type_is_mapping(type));
595 genpaddr_t genpaddr = local_phys_to_gen_phys(lpaddr);
597 debug(SUBSYS_CAPS, "creating caps for %#"PRIxGENPADDR
598 ", %" PRIuGENSIZE " bytes, objsize=%"PRIuGENSIZE
599 ", count=%zu, owner=%d, type=%d\n",
600 genpaddr, size, objsize, count, (int)owner, (int)type);
602 // Virtual address of the memory the kernel object resides in
603 // XXX: A better of doing this,
604 // this is creating caps that the kernel cannot address.
605 // It assumes that the cap is not of the type which will have to zeroed out.
607 if(lpaddr < PADDR_SPACE_LIMIT) {
608 lvaddr = local_phys_to_mem(lpaddr);
613 /* Initialize the created capability */
614 struct capability temp_cap;
615 memset(&temp_cap, 0, sizeof(struct capability));
616 temp_cap.type = type;
617 // XXX: Handle rights!
618 temp_cap.rights = CAPRIGHTS_ALLRIGHTS;
620 debug(SUBSYS_CAPS, "owner = %d, my_core_id = %d\n", owner, my_core_id);
621 if (owner == my_core_id) {
622 // If we're creating new local objects, they need to be cleared
623 err = caps_zero_objects(type, lpaddr, objsize, count);
624 if (err_is_fail(err)) {
632 /* Set the type specific fields and insert into #dest_caps */
635 for(dest_i = 0; dest_i < count; dest_i++) {
636 // Initialize type specific fields
637 temp_cap.u.frame.base = genpaddr + dest_i * objsize;
638 temp_cap.u.frame.bytes = objsize;
639 assert((get_size(&temp_cap) & BASE_PAGE_MASK) == 0);
640 // Insert the capability
641 err = set_cap(&dest_caps[dest_i].cap, &temp_cap);
642 if (err_is_fail(err)) {
648 case ObjType_PhysAddr:
649 for(dest_i = 0; dest_i < count; dest_i++) {
650 // Initialize type specific fields
651 temp_cap.u.physaddr.base = genpaddr + dest_i * objsize;
652 temp_cap.u.physaddr.bytes = objsize;
653 // Insert the capability
654 err = set_cap(&dest_caps[dest_i].cap, &temp_cap);
655 if (err_is_fail(err)) {
662 for(dest_i = 0; dest_i < count; dest_i++) {
663 // Initialize type specific fields
664 temp_cap.u.ram.base = genpaddr + dest_i * objsize;
665 temp_cap.u.ram.bytes = objsize;
666 // Insert the capabilities
667 err = set_cap(&dest_caps[dest_i].cap, &temp_cap);
668 if (err_is_fail(err)) {
674 case ObjType_DevFrame:
675 for(dest_i = 0; dest_i < count; dest_i++) {
676 // Initialize type specific fields
677 temp_cap.u.devframe.base = genpaddr + dest_i * objsize;
678 temp_cap.u.devframe.bytes = objsize;
679 // Insert the capabilities
680 err = set_cap(&dest_caps[dest_i].cap, &temp_cap);
681 if (err_is_fail(err)) {
687 case ObjType_L1CNode:
688 for (dest_i = 0; dest_i < count; dest_i++) {
689 assert(objsize >= OBJSIZE_L2CNODE);
690 assert(objsize % OBJSIZE_L2CNODE == 0);
691 temp_cap.u.l1cnode.cnode = lpaddr + dest_i * objsize;
692 temp_cap.u.l1cnode.allocated_bytes = objsize;
693 // XXX: implement CNode cap rights
694 temp_cap.u.l1cnode.rightsmask = CAPRIGHTS_ALLRIGHTS;
695 err = set_cap(&dest_caps[dest_i].cap, &temp_cap);
696 if (err_is_fail(err)) {
702 case ObjType_L2CNode:
703 for (dest_i = 0; dest_i < count; dest_i++) {
704 temp_cap.u.l2cnode.cnode = lpaddr + dest_i * objsize;
705 // XXX: implement CNode cap rights
706 temp_cap.u.l2cnode.rightsmask = CAPRIGHTS_ALLRIGHTS;
707 err = set_cap(&dest_caps[dest_i].cap, &temp_cap);
708 if (err_is_fail(err)) {
714 case ObjType_VNode_ARM_l1:
716 size_t objsize_vnode = vnode_objsize(type);
718 for(dest_i = 0; dest_i < count; dest_i++) {
719 // Initialize type specific fields
720 temp_cap.u.vnode_arm_l1.base =
721 genpaddr + dest_i * objsize_vnode;
724 // Insert the capability
725 err = set_cap(&dest_caps[dest_i].cap, &temp_cap);
726 if (err_is_fail(err)) {
734 case ObjType_VNode_ARM_l2:
736 size_t objsize_vnode = vnode_objsize(type);
738 for(dest_i = 0; dest_i < count; dest_i++) {
739 // Initialize type specific fields
740 temp_cap.u.vnode_arm_l2.base =
741 genpaddr + dest_i * objsize_vnode;
743 // Insert the capability
744 err = set_cap(&dest_caps[dest_i].cap, &temp_cap);
745 if (err_is_fail(err)) {
752 case ObjType_VNode_AARCH64_l0:
754 size_t objsize_vnode = vnode_objsize(type);
756 for(dest_i = 0; dest_i < count; dest_i++) {
757 // Initialize type specific fields
758 temp_cap.u.vnode_aarch64_l0.base =
759 genpaddr + dest_i * objsize_vnode;
761 // Insert the capability
762 err = set_cap(&dest_caps[dest_i].cap, &temp_cap);
763 if (err_is_fail(err)) {
771 case ObjType_VNode_AARCH64_l1:
773 size_t objsize_vnode = vnode_objsize(type);
775 for(dest_i = 0; dest_i < count; dest_i++) {
776 // Initialize type specific fields
777 temp_cap.u.vnode_aarch64_l1.base =
778 genpaddr + dest_i * objsize_vnode;
780 // Insert the capability
781 err = set_cap(&dest_caps[dest_i].cap, &temp_cap);
782 if (err_is_fail(err)) {
790 case ObjType_VNode_AARCH64_l2:
792 size_t objsize_vnode = vnode_objsize(type);
794 for(dest_i = 0; dest_i < count; dest_i++) {
795 // Initialize type specific fields
796 temp_cap.u.vnode_aarch64_l2.base =
797 genpaddr + dest_i * objsize_vnode;
799 // Insert the capability
800 err = set_cap(&dest_caps[dest_i].cap, &temp_cap);
802 if (err_is_fail(err)) {
809 case ObjType_VNode_AARCH64_l3:
811 size_t objsize_vnode = vnode_objsize(type);
813 for(dest_i = 0; dest_i < count; dest_i++) {
814 // Initialize type specific fields
815 temp_cap.u.vnode_aarch64_l3.base =
816 genpaddr + dest_i * objsize_vnode;
818 // Insert the capability
819 err = set_cap(&dest_caps[dest_i].cap, &temp_cap);
820 if (err_is_fail(err)) {
827 case ObjType_VNode_x86_32_ptable:
829 size_t objsize_vnode = vnode_objsize(type);
831 for(dest_i = 0; dest_i < count; dest_i++) {
832 // Initialize type specific fields
833 temp_cap.u.vnode_x86_32_ptable.base =
834 genpaddr + dest_i * objsize_vnode;
836 // Insert the capability
837 err = set_cap(&dest_caps[dest_i].cap, &temp_cap);
838 if (err_is_fail(err)) {
845 case ObjType_VNode_x86_32_pdir:
847 size_t objsize_vnode = vnode_objsize(type);
849 for(dest_i = 0; dest_i < count; dest_i++) {
850 // Initialize type specific fields
851 temp_cap.u.vnode_x86_32_pdir.base =
852 genpaddr + dest_i * objsize_vnode;
854 #if defined(__i386__) && !defined(CONFIG_PAE)
855 // Make it a good PDE by inserting kernel/mem VSpaces
856 lpaddr = gen_phys_to_local_phys(temp_cap.u.vnode_x86_32_pdir.base);
857 paging_x86_32_make_good_pdir(lpaddr);
860 // Insert the capability
861 err = set_cap(&dest_caps[dest_i].cap, &temp_cap);
862 if (err_is_fail(err)) {
869 case ObjType_VNode_x86_32_pdpt:
871 size_t objsize_vnode = vnode_objsize(type);
873 for(dest_i = 0; dest_i < count; dest_i++) {
874 // Initialize type specific fields
875 temp_cap.u.vnode_x86_32_pdir.base =
876 genpaddr + dest_i * objsize_vnode;
878 #if defined(__i386__) && defined(CONFIG_PAE)
879 // Make it a good PDPTE by inserting kernel/mem VSpaces
881 gen_phys_to_local_phys(temp_cap.u.vnode_x86_32_pdpt.base);
882 paging_x86_32_make_good_pdpte(var);
885 // Insert the capability
886 err = set_cap(&dest_caps[dest_i].cap, &temp_cap);
887 if (err_is_fail(err)) {
894 case ObjType_VNode_x86_64_ptable:
896 size_t objsize_vnode = vnode_objsize(type);
898 for(dest_i = 0; dest_i < count; dest_i++) {
899 // Initialize type specific fields
900 temp_cap.u.vnode_x86_64_ptable.base =
901 genpaddr + dest_i * objsize_vnode;
903 // Insert the capability
904 err = set_cap(&dest_caps[dest_i].cap, &temp_cap);
905 if (err_is_fail(err)) {
912 case ObjType_VNode_x86_64_pdir:
914 size_t objsize_vnode = vnode_objsize(type);
916 for(dest_i = 0; dest_i < count; dest_i++) {
917 // Initialize type specific fields
918 temp_cap.u.vnode_x86_64_pdir.base =
919 genpaddr + dest_i * objsize_vnode;
921 // Insert the capability
922 err = set_cap(&dest_caps[dest_i].cap, &temp_cap);
923 if (err_is_fail(err)) {
930 case ObjType_VNode_x86_64_pdpt:
932 size_t objsize_vnode = vnode_objsize(type);
934 for(dest_i = 0; dest_i < count; dest_i++) {
935 // Initialize type specific fields
936 temp_cap.u.vnode_x86_64_pdpt.base =
937 genpaddr + dest_i * objsize_vnode;
939 // Insert the capability
940 err = set_cap(&dest_caps[dest_i].cap, &temp_cap);
941 if (err_is_fail(err)) {
948 case ObjType_VNode_x86_64_pml4:
950 size_t objsize_vnode = vnode_objsize(type);
952 for(dest_i = 0; dest_i < count; dest_i++) {
953 // Initialize type specific fields
954 temp_cap.u.vnode_x86_64_pml4.base =
955 genpaddr + dest_i * objsize_vnode;
957 #if defined(__x86_64__) || defined(__k1om__)
958 // Make it a good PML4 by inserting kernel/mem VSpaces
959 lpaddr_t var = gen_phys_to_local_phys(get_address(&temp_cap));
960 paging_x86_64_make_good_pml4(var);
963 // Insert the capability
964 err = set_cap(&dest_caps[dest_i].cap, &temp_cap);
965 if (err_is_fail(err)) {
973 case ObjType_Dispatcher:
974 assert(OBJSIZE_DISPATCHER >= sizeof(struct dcb));
976 for(dest_i = 0; dest_i < count; dest_i++) {
977 // Initialize type specific fields
978 temp_cap.u.dispatcher.dcb = (struct dcb *)
979 (lvaddr + dest_i * OBJSIZE_DISPATCHER);
980 // Insert the capability
981 err = set_cap(&dest_caps[dest_i].cap, &temp_cap);
982 if (err_is_fail(err)) {
989 // ID type does not refer to a kernel object
992 assert(objsize == 0);
995 // Prevent wrap around
996 if (id_cap_counter >= UINT32_MAX) {
997 return SYS_ERR_ID_SPACE_EXHAUSTED;
1000 // Generate a new ID, core_local_id monotonically increases
1001 temp_cap.u.id.coreid = my_core_id;
1002 temp_cap.u.id.core_local_id = id_cap_counter++;
1004 // Insert the capability
1005 err = set_cap(&dest_caps->cap, &temp_cap);
1009 temp_cap.u.io.start = 0;
1010 temp_cap.u.io.end = 65535;
1013 case ObjType_Kernel:
1015 case ObjType_IRQTable:
1016 case ObjType_IRQDest:
1017 case ObjType_IRQSrc:
1018 case ObjType_EndPoint:
1019 case ObjType_Notify_IPI:
1020 case ObjType_PerfMon:
1021 // These types do not refer to a kernel object
1022 assert(lpaddr == 0);
1024 assert(objsize == 0);
1027 // Insert the capability
1028 err = set_cap(&dest_caps->cap, &temp_cap);
1029 if (err_is_ok(err)) {
1034 case ObjType_KernelControlBlock:
1035 assert(OBJSIZE_KCB >= sizeof(struct kcb));
1037 for(size_t i = 0; i < count; i++) {
1038 // Initialize type specific fields
1039 temp_cap.u.kernelcontrolblock.kcb = (struct kcb *)
1040 (lvaddr + i * OBJSIZE_KCB);
1041 // Insert the capability
1042 err = set_cap(&dest_caps[i].cap, &temp_cap);
1043 if (err_is_fail(err)) {
1050 panic("Unhandled capability type or capability of this type cannot"
1054 if (err_is_fail(err)) {
1055 // Revert the partially initialized caps to zero
1056 for (size_t i = 0; i < dest_i; i++) {
1057 memset(&dest_caps[i], 0, sizeof(dest_caps[i]));
1062 // Set the owner for all the new caps
1063 for (size_t i = 0; i < dest_i; i++) {
1064 dest_caps[i].mdbnode.owner = owner;
1072 * Look up a capability in two-level cspace rooted at `rootcn`.
1074 errval_t caps_lookup_slot(struct capability *rootcn, capaddr_t cptr,
1075 uint8_t level, struct cte **ret, CapRights rights)
1077 TRACE(KERNEL, CAP_LOOKUP_SLOT, 0);
1079 cslot_t l1index, l2index;
1080 l1index = (cptr >> L2_CNODE_BITS) & MASK(CPTR_BITS-L2_CNODE_BITS);
1081 l2index = cptr & MASK(L2_CNODE_BITS);
1083 assert(ret != NULL);
1084 assert(rootcn != NULL);
1087 debug(SUBSYS_CAPS, "%s called with level=%hhu, from %p\n",
1088 __FUNCTION__, level,
1089 (void*)kernel_virt_to_elf_addr(__builtin_return_address(0)));
1090 TRACE(KERNEL, CAP_LOOKUP_SLOT, 1);
1091 return SYS_ERR_CAP_LOOKUP_DEPTH;
1095 // level 0 means that we do not do any resolution and just return the cte
1098 *ret = cte_for_cap(rootcn);
1099 TRACE(KERNEL, CAP_LOOKUP_SLOT, 1);
1103 if (rootcn->type != ObjType_L1CNode) {
1104 debug(SUBSYS_CAPS, "%s: rootcn->type = %d, called from %p\n",
1105 __FUNCTION__, rootcn->type,
1106 (void*)kernel_virt_to_elf_addr(__builtin_return_address(0)));
1107 TRACE(KERNEL, CAP_LOOKUP_SLOT, 1);
1108 // XXX: think about errors
1109 return SYS_ERR_CNODE_TYPE;
1111 assert(rootcn->type == ObjType_L1CNode);
1113 if (l1index > cnode_get_slots(rootcn)) {
1114 TRACE(KERNEL, CAP_LOOKUP_SLOT, 1);
1115 return SYS_ERR_L1_CNODE_INDEX;
1118 /* Apply rights to L1 CNode */
1119 if ((rootcn->rights & rights) != rights) {
1120 debug(SUBSYS_CAPS, "caps_lookup_slot: Rights mismatch\n"
1121 "Passed rights = %u, cnode_cap->rights = %u\n",
1122 rights, rootcn->rights);
1123 TRACE(KERNEL, CAP_LOOKUP_SLOT, 1);
1124 return SYS_ERR_CNODE_RIGHTS;
1127 struct cte *l2cnode = caps_locate_slot(get_address(rootcn), l1index);
1129 // level == 1 means that we terminate after looking up the slot in the L1
1132 if (l2cnode->cap.type == ObjType_Null) {
1133 TRACE(KERNEL, CAP_LOOKUP_SLOT, 1);
1134 return SYS_ERR_CAP_NOT_FOUND;
1137 TRACE(KERNEL, CAP_LOOKUP_SLOT, 1);
1141 // L2 CNode in given L1 slot does not exist
1142 if (l2cnode->cap.type == ObjType_Null) {
1143 TRACE(KERNEL, CAP_LOOKUP_SLOT, 1);
1144 debug(SUBSYS_CAPS, "%s: l2cnode is NULL\n", __FUNCTION__);
1145 return SYS_ERR_CNODE_NOT_FOUND;
1147 if (l2cnode->cap.type != ObjType_L2CNode) {
1148 TRACE(KERNEL, CAP_LOOKUP_SLOT, 1);
1149 debug(SUBSYS_CAPS, "%s: l2cnode->type = %d\n", __FUNCTION__,
1151 return SYS_ERR_CNODE_TYPE;
1153 assert(l2cnode->cap.type == ObjType_L2CNode);
1155 assert(l2index < L2_CNODE_SLOTS);
1157 /* Apply rights to L2 CNode */
1158 if ((l2cnode->cap.rights & rights) != rights) {
1159 debug(SUBSYS_CAPS, "caps_lookup_slot: Rights mismatch\n"
1160 "Passed rights = %u, cnode_cap->rights = %u\n",
1161 rights, l2cnode->cap.rights);
1162 TRACE(KERNEL, CAP_LOOKUP_SLOT, 1);
1163 return SYS_ERR_CNODE_RIGHTS;
1166 struct cte *cte = caps_locate_slot(get_address(&l2cnode->cap), l2index);
1167 if (cte->cap.type == ObjType_Null) {
1168 TRACE(KERNEL, CAP_LOOKUP_SLOT, 1);
1169 return SYS_ERR_CAP_NOT_FOUND;
1174 TRACE(KERNEL, CAP_LOOKUP_SLOT, 1);
1179 * Wrapper for caps_lookup_slot returning capability instead of cte.
1181 errval_t caps_lookup_cap(struct capability *cnode_cap, capaddr_t cptr,
1182 uint8_t level, struct capability **ret, CapRights rights)
1184 TRACE(KERNEL, CAP_LOOKUP_CAP, 0);
1186 struct cte *ret_cte;
1187 errval_t err = caps_lookup_slot(cnode_cap, cptr, level, &ret_cte, rights);
1188 if (err_is_fail(err)) {
1191 *ret = &ret_cte->cap;
1192 TRACE(KERNEL, CAP_LOOKUP_CAP, 1);
1197 * \brief Create a capability from an existing capability metadata.
1199 * Used when sending capabilities across cores. The metadata is sent across
1200 * cores and the receiving monitor can create the new capability on its core.
1202 * \bug Does not check that supplied owner matches existing copies of cap.
1204 errval_t caps_create_from_existing(struct capability *root, capaddr_t cnode_cptr,
1205 int cnode_level, cslot_t dest_slot, coreid_t owner,
1206 struct capability *src)
1208 TRACE(KERNEL, CAP_CREATE_FROM_EXISTING, 0);
1210 struct capability *cnode;
1211 err = caps_lookup_cap(root, cnode_cptr, cnode_level, &cnode,
1212 CAPRIGHTS_READ_WRITE);
1213 if (err_is_fail(err)) {
1214 return err_push(err, SYS_ERR_SLOT_LOOKUP_FAIL);
1216 if (cnode->type != ObjType_L1CNode &&
1217 cnode->type != ObjType_L2CNode)
1219 return SYS_ERR_CNODE_TYPE;
1222 struct cte *dest = caps_locate_slot(get_address(cnode), dest_slot);
1224 err = set_cap(&dest->cap, src);
1225 if (err_is_fail(err)) {
1229 dest->mdbnode.owner = owner;
1231 err = mdb_insert(dest);
1232 assert(err_is_ok(err));
1234 struct cte *neighbour = NULL;
1236 && (neighbour = mdb_predecessor(dest))
1237 && !is_copy(&dest->cap, &neighbour->cap))
1242 && (neighbour = mdb_successor(dest))
1243 && !is_copy(&dest->cap, &neighbour->cap))
1249 assert(!neighbour->mdbnode.in_delete);
1250 assert(neighbour->mdbnode.owner == owner);
1251 #define CP_ATTR(a) dest->mdbnode.a = neighbour->mdbnode.a
1253 CP_ATTR(remote_copies);
1254 CP_ATTR(remote_ancs);
1255 CP_ATTR(remote_descs);
1259 dest->mdbnode.locked = false;
1260 if (owner != my_core_id) {
1261 // For foreign caps it does not really matter if ancestors or
1262 // descendants exist
1263 dest->mdbnode.remote_copies = true;
1264 dest->mdbnode.remote_ancs = false;
1265 dest->mdbnode.remote_descs = false;
1268 // We just created a new copy of a owned capability from nothing.
1269 // This is either caused by a retype, or by sharing a capability
1270 // that does not care about locality.
1271 // XXX: This should probably be done more explicitly -MN
1272 if (distcap_needs_locality(dest->cap.type)) {
1273 // Retype, so have ancestors and no descendants
1274 dest->mdbnode.remote_copies = false;
1275 dest->mdbnode.remote_ancs = true;
1276 dest->mdbnode.remote_descs = false;
1279 dest->mdbnode.remote_copies = false;
1280 dest->mdbnode.remote_ancs = false;
1281 dest->mdbnode.remote_descs = false;
1286 TRACE_CAP_MSG("created", dest);
1288 TRACE(KERNEL, CAP_CREATE_FROM_EXISTING, 1);
1292 //{{{1 Capability creation
1294 /// check arguments, return true iff ok
1295 STATIC_ASSERT(48 == ObjType_Num, "Knowledge of all cap types");
1296 static bool check_caps_create_arguments(enum objtype type,
1297 size_t bytes, size_t objsize,
1300 gensize_t base_mask = BASE_PAGE_MASK;
1301 if (type_is_vnode(type)) {
1302 base_mask = vnode_objsize(type) - 1;
1304 /* mappable types need to be at least BASE_PAGE_SIZEd */
1305 if (type_is_mappable(type)) {
1306 /* source size not multiple of or not aligned to BASE_PAGE_SIZE */
1307 if (bytes & base_mask) {
1308 debug(SUBSYS_CAPS, "source size not multiple of BASE_PAGE_SIZE\n");
1311 /* objsize > 0 and not multiple of BASE_PAGE_SIZE */
1312 if (objsize > 0 && objsize & base_mask) {
1313 debug(SUBSYS_CAPS, "object size not multiple of BASE_PAGE_SIZE\n");
1317 /* check that bytes can be evenly divided into objsize sized chunks */
1318 if (exact && bytes > 0 && objsize > 0) {
1319 if (bytes % objsize) {
1320 debug(SUBSYS_CAPS, "source size cannot be evenly divided into object size-sized chunks\n");
1322 return bytes % objsize == 0;
1328 if (type == ObjType_L1CNode) {
1329 /* L1 CNode minimum size is OBJSIZE_L2CNODE */
1330 if (bytes < OBJSIZE_L2CNODE || objsize < OBJSIZE_L2CNODE) {
1331 debug(SUBSYS_CAPS, "source size or L1 CNode objsize < OBJSIZE_L2CNODE\n");
1334 /* check that bytes can be evenly divided into L1 CNodes of objsize */
1335 if (exact && (bytes % objsize != 0)) {
1336 debug(SUBSYS_CAPS, "source not evenly divisible into L1 CNodes of objsize\n");
1339 /* L1 CNode size must be multiple of 1UL << OBJBITS_CTE */
1340 return objsize % (1UL << OBJBITS_CTE) == 0;
1343 if (type == ObjType_L2CNode) {
1344 /* L2 CNode size must be OBJSIZE_L2CNODE */
1345 if (bytes < OBJSIZE_L2CNODE || objsize != OBJSIZE_L2CNODE) {
1346 debug(SUBSYS_CAPS, "source size < or L2 CNode objsize != OBJSIZE_L2CNODE\n");
1349 if (exact && (bytes % objsize != 0)) {
1350 debug(SUBSYS_CAPS, "source not evenly divisible into L2 CNodes of objsize\n");
1356 /* special case Dispatcher which is 1kB right now */
1357 if (type == ObjType_Dispatcher) {
1358 if (bytes & (OBJSIZE_DISPATCHER - 1)) {
1361 if (objsize > 0 && objsize != OBJSIZE_DISPATCHER) {
1368 // All other types do not need special alignments/offsets
1372 /** Create caps to new kernel objects.
1373 * This takes the size of the memory region in bytes, and the size of
1374 * individual objects in bytes. The following needs to hold:
1375 * bytes % objbytes == 0
1377 errval_t caps_create_new(enum objtype type, lpaddr_t addr, size_t bytes,
1378 size_t objsize, coreid_t owner, struct cte *caps)
1380 TRACE(KERNEL, CAP_CREATE_NEW, 0);
1381 /* Parameter checking */
1382 assert(type != ObjType_EndPoint); // Cap of this type cannot be created
1383 debug(SUBSYS_CAPS, "caps_create_new: type = %d, addr = %#"PRIxLPADDR
1384 ", bytes=%zu, objsize=%zu\n", type, addr, bytes, objsize);
1385 assert(check_caps_create_arguments(type, bytes, objsize, false));
1386 assert(addr == 0 || check_caps_create_arguments(type, bytes, objsize, true));
1388 size_t numobjs = caps_max_numobjs(type, bytes, objsize);
1389 assert(numobjs > 0);
1390 // XXX: Dispatcher creation is kind of hacky right now :(
1391 // Consider allowing non-mappable types to be < BASE_PAGE_SIZE
1392 //if (type == ObjType_Dispatcher) {
1396 /* Create the new capabilities */
1397 errval_t err = caps_create(type, addr, bytes, objsize, numobjs, owner, caps);
1398 if (err_is_fail(err)) {
1402 // Handle the mapping database
1403 set_init_mapping(caps, numobjs);
1405 TRACE_CAP_MSG("created", &caps[0]);
1407 TRACE(KERNEL, CAP_CREATE_NEW, 1);
1411 STATIC_ASSERT(48 == ObjType_Num, "Knowledge of all cap types");
1413 /// Create `count` new caps of `type` from `offset` in src, and put them in
1414 /// `dest_cnode` starting at `dest_slot`.
1415 errval_t caps_retype(enum objtype type, gensize_t objsize, size_t count,
1416 struct capability *dest_cnode, cslot_t dest_slot,
1417 struct cte *src_cte, gensize_t offset,
1420 TRACE(KERNEL, CAP_RETYPE, 0);
1422 genpaddr_t base = 0;
1425 bool do_range_check = false;
1427 /* Parameter checking */
1428 assert(type != ObjType_Null);
1429 assert(type < ObjType_Num);
1430 if (type == ObjType_Null || type >= ObjType_Num) {
1431 return SYS_ERR_INVALID_RETYPE;
1434 debug(SUBSYS_CAPS, "%s: Retyping to type=%d, from offset=%" PRIuGENSIZE
1435 ", objsize=%" PRIuGENSIZE ", count=%zu\n",
1436 __FUNCTION__, type, offset, objsize, count);
1438 /* check that offset into source cap is multiple of BASE_PAGE_SIZE */
1439 if (offset % BASE_PAGE_SIZE != 0) {
1440 return SYS_ERR_RETYPE_INVALID_OFFSET;
1442 assert(offset % BASE_PAGE_SIZE == 0);
1444 // check that size is multiple of BASE_PAGE_SIZE for mappable types
1445 gensize_t base_size = BASE_PAGE_SIZE;
1446 if (type_is_vnode(type)) {
1447 base_size = vnode_objsize(type);
1449 if (type_is_mappable(type) && objsize % base_size != 0) {
1450 debug(SUBSYS_CAPS, "%s: objsize = %"PRIuGENSIZE"\n", __FUNCTION__, objsize);
1451 return SYS_ERR_INVALID_SIZE;
1453 else if (type == ObjType_L1CNode && objsize % OBJSIZE_L2CNODE != 0)
1455 printk(LOG_WARN, "%s: CNode: objsize = %" PRIuGENSIZE "\n", __FUNCTION__, objsize);
1456 return SYS_ERR_INVALID_SIZE;
1458 else if (type == ObjType_L2CNode && objsize != OBJSIZE_L2CNODE)
1460 printk(LOG_WARN, "%s: L2CNode: objsize = %"PRIuGENSIZE"\n", __FUNCTION__, objsize);
1461 return SYS_ERR_INVALID_SIZE;
1463 assert((type_is_mappable(type) && objsize % base_size == 0) ||
1464 (type == ObjType_L1CNode && objsize % OBJSIZE_L2CNODE == 0 &&
1465 objsize >= OBJSIZE_L2CNODE) ||
1466 (type == ObjType_L2CNode && objsize == OBJSIZE_L2CNODE) ||
1467 !type_is_mappable(type));
1469 /* No explicit retypes to Mapping allowed */
1470 if (type_is_mapping(type)) {
1471 return SYS_ERR_RETYPE_MAPPING_EXPLICIT;
1474 struct capability *src_cap = &src_cte->cap;
1476 TRACE_CAP_MSG("retyping", src_cte);
1478 /* Check retypability */
1479 err = is_retypeable(src_cte, src_cap->type, type, from_monitor);
1480 if (err_is_fail(err)) {
1481 if (err_no(err) != SYS_ERR_REVOKE_FIRST) {
1482 printk(LOG_NOTE, "caps_retype: is_retypeable failed: %"PRIuERRV"\n", err);
1483 debug(SUBSYS_CAPS, "caps_retype: is_retypeable failed\n");
1487 "caps_retype: is_retypeable() returned SYS_ERR_REVOKE_FIRST, doing range check\n");
1488 // We handle err_revoke_first fine-grained checking below, as it
1489 // might happen for non-overlapping regions.
1491 // TODO: move the range checking into is_retypeable() or even
1492 // is_revoked_first(), -SG 2016-04-18
1493 do_range_check = true;
1496 // from here: src cap type is one of these.
1497 assert(src_cap->type == ObjType_PhysAddr ||
1498 src_cap->type == ObjType_RAM ||
1499 src_cap->type == ObjType_Dispatcher ||
1500 src_cap->type == ObjType_Frame ||
1501 src_cap->type == ObjType_DevFrame);
1503 if (src_cap->type != ObjType_Dispatcher) {
1504 base = get_address(src_cap);
1505 size = get_size(src_cap);
1508 maxobjs = caps_max_numobjs(type, get_size(src_cap), objsize);
1509 debug(SUBSYS_CAPS, "maximum possible new object count: %zu\n", maxobjs);
1512 debug(SUBSYS_CAPS, "caps_retype: maxobjs == 0\n");
1513 return SYS_ERR_INVALID_SIZE;
1516 if (count > maxobjs) {
1517 debug(SUBSYS_CAPS, "caps_retype: maxobjs = %zu, count = %zu\n", maxobjs, count);
1518 return SYS_ERR_RETYPE_INVALID_COUNT;
1520 // from here: count <= maxobjs
1521 assert(count <= maxobjs);
1522 // make sure nobody calls with the old behaviour
1524 return SYS_ERR_RETYPE_INVALID_COUNT;
1528 /* check that we can create `count` objs from `offset` in source, and
1529 * update base accordingly */
1530 if (src_cap->type != ObjType_Dispatcher) {
1531 // TODO: convince ourselves that this is the only condition on offset
1532 if (offset + count * objsize > get_size(src_cap)) {
1533 debug(SUBSYS_CAPS, "caps_retype: cannot create all %zu objects"
1534 " of size 0x%" PRIxGENSIZE " from offset 0x%" PRIxGENSIZE "\n",
1535 count, objsize, offset);
1536 return SYS_ERR_RETYPE_INVALID_OFFSET;
1538 // adjust base address for new objects
1541 // Check whether we got SYS_ERR_REVOKE_FIRST because of
1542 // non-overlapping child
1543 if (do_range_check) {
1544 int find_range_result = 0;
1545 struct cte *found_cte = NULL;
1546 err = mdb_find_range(get_type_root(src_cap->type), base, objsize * count,
1547 MDB_RANGE_FOUND_SURROUNDING, &found_cte, &find_range_result);
1548 // this should never return an error unless we mess up the
1549 // non-user supplied arguments
1550 if (err_is_fail(err)) {
1551 printk(LOG_WARN, "mdb_find_range returned: %"PRIuERRV"\n", err);
1553 assert(err_is_ok(err));
1554 // return REVOKE_FIRST, if we found a cap inside the region
1555 // (FOUND_INNER == 2) or overlapping the region (FOUND_PARTIAL == 3)
1556 if (find_range_result >= MDB_RANGE_FOUND_INNER) {
1558 "%s: found existing region inside, or overlapping requested region:\n",
1560 return SYS_ERR_REVOKE_FIRST;
1562 // return REVOKE_FIRST, if we found a cap that isn't our source
1563 // (or a copy of our source) covering the whole requested region.
1564 else if (find_range_result == MDB_RANGE_FOUND_SURROUNDING &&
1565 !is_copy(&found_cte->cap, src_cap))
1568 "%s: found non source region fully covering requested region\n",
1570 return SYS_ERR_REVOKE_FIRST;
1575 /* check that destination slots all fit within target cnode */
1576 if (dest_slot + count > cnode_get_slots(dest_cnode)) {
1577 debug(SUBSYS_CAPS, "caps_retype: dest slots don't fit in cnode\n");
1578 return SYS_ERR_SLOTS_INVALID;
1581 /* check that destination slots are all empty */
1582 debug(SUBSYS_CAPS, "caps_retype: dest cnode is %#" PRIxLPADDR
1584 get_address(dest_cnode), (int)dest_slot);
1585 for (cslot_t i = 0; i < count; i++) {
1586 if (caps_locate_slot(get_address(dest_cnode), dest_slot + i)->cap.type
1588 debug(SUBSYS_CAPS, "caps_retype: dest slot %d in use\n",
1589 (int)(dest_slot + i));
1590 return SYS_ERR_SLOTS_IN_USE;
1594 /* Check that L1 CNode is destination when creating L2 CNode */
1595 if (type == ObjType_L2CNode) {
1596 debug(SUBSYS_CAPS, "caps_retype: check that dest cnode is L1"
1597 " when creating L2 CNodes\n");
1598 if (dest_cnode->type != ObjType_L1CNode &&
1599 dest_cnode->type != ObjType_L2CNode)
1601 panic("L2 CNode can only be created in L1 or L2 CNode\n");
1605 /* create new caps */
1606 struct cte *dest_cte =
1607 caps_locate_slot(get_address(dest_cnode), dest_slot);
1608 err = caps_create(type, base, size, objsize, count, my_core_id, dest_cte);
1609 if (err_is_fail(err)) {
1610 debug(SUBSYS_CAPS, "caps_retype: failed to create a dest cap\n");
1611 return err_push(err, SYS_ERR_RETYPE_CREATE);
1614 /* special initialisation for endpoint caps */
1615 if (type == ObjType_EndPoint) {
1616 assert(src_cap->type == ObjType_Dispatcher);
1618 struct capability *dest_cap = &dest_cte->cap;
1619 dest_cap->u.endpoint.listener = src_cap->u.dispatcher.dcb;
1622 /* Handle mapping */
1623 for (size_t i = 0; i < count; i++) {
1624 mdb_insert(&dest_cte[i]);
1627 #ifdef TRACE_PMEM_CAPS
1628 for (size_t i = 0; i < count; i++) {
1629 TRACE_CAP_MSG("created", &dest_cte[i]);
1633 TRACE(KERNEL, CAP_RETYPE, 1);
1637 /// Check the validity of a retype operation
1638 errval_t is_retypeable(struct cte *src_cte, enum objtype src_type,
1639 enum objtype dest_type, bool from_monitor)
1641 if (!is_well_founded(src_type, dest_type)) {
1642 return SYS_ERR_INVALID_RETYPE;
1643 } else if (!is_revoked_first(src_cte, src_type)){
1644 //printf("err_revoke_first: (%p, %d, %d)\n", src_cte, src_type, dest_type);
1645 return SYS_ERR_REVOKE_FIRST;
1646 } else if (dest_type == ObjType_EndPoint && src_cte->mdbnode.owner == my_core_id) {
1647 // XXX: because of the current "multi-retype" hack for endpoints, a
1648 // dispatcher->endpoint retype can happen irrespective of the existence
1649 // of descendants on any core.
1650 // However, we only do this for locally owned caps as the owner should
1651 // be notified that the cap has remote descendants
1653 } else if (!from_monitor && (src_cte->mdbnode.owner != my_core_id
1654 || src_cte->mdbnode.remote_descs)) {
1655 return SYS_ERR_RETRY_THROUGH_MONITOR;
1661 /// Create copies to a slot within a cnode
1662 errval_t caps_copy_to_cnode(struct cte *dest_cnode_cte, cslot_t dest_slot,
1663 struct cte *src_cte, bool mint, uintptr_t param1,
1666 /* Parameter Checking */
1667 assert(dest_cnode_cte->cap.type == ObjType_L1CNode ||
1668 dest_cnode_cte->cap.type == ObjType_L2CNode);
1670 // only allow L2 CNodes and BSP KCB in L1 CNode
1671 // XXX: BSPKCB should not be in rootcn...
1672 if (dest_cnode_cte->cap.type == ObjType_L1CNode &&
1673 src_cte->cap.type != ObjType_L2CNode &&
1674 src_cte->cap.type != ObjType_KernelControlBlock)
1676 printk(LOG_WARN, "trying to copy cap type %d into cap type %d\n",
1677 src_cte->cap.type, dest_cnode_cte->cap.type);
1678 return SYS_ERR_DEST_TYPE_INVALID;
1681 struct cte *dest_cte;
1682 dest_cte = caps_locate_slot(get_address(&dest_cnode_cte->cap), dest_slot);
1683 return caps_copy_to_cte(dest_cte, src_cte, mint, param1, param2);
1687 /// Create copies to a cte
1688 STATIC_ASSERT(48 == ObjType_Num, "Knowledge of all cap types");
1689 errval_t caps_copy_to_cte(struct cte *dest_cte, struct cte *src_cte, bool mint,
1690 uintptr_t param1, uintptr_t param2)
1693 /* Parameter checking */
1695 assert(dest_cte != NULL);
1696 assert(src_cte != NULL);
1698 struct capability *src_cap = &src_cte->cap;
1699 struct capability *dest_cap = &dest_cte->cap;
1700 // NULL caps cannot be copied/minted
1701 if (src_cap->type == ObjType_Null) {
1702 return SYS_ERR_CAP_NOT_FOUND;
1704 // Parameters should be 0 if not minting
1706 assert(param1 == 0);
1707 assert(param2 == 0);
1710 assert(!src_cte->mdbnode.in_delete);
1712 /* Insert #source_cap into #dest_cap */
1713 err = set_cap(dest_cap, src_cap);
1714 if (err_is_fail(err)) {
1718 /* Transfer MDB attributes that must be equal for all copies */
1719 #define CP_ATTR(at) dest_cte->mdbnode.at = src_cte->mdbnode.at
1722 CP_ATTR(remote_copies);
1723 CP_ATTR(remote_ancs);
1724 CP_ATTR(remote_descs);
1729 TRACE_CAP_MSG("copied to", dest_cte);
1730 // Handle mapping here only for non-mint operations
1731 // (mint can change eq fields which would make the early insertion
1732 // invalid in some cases)
1733 mdb_insert(dest_cte);
1737 TRACE_CAP_MSG("minting to", dest_cte);
1740 /* For minting, set the specified parameters */
1741 // Process source-specific parameters for minting
1742 // XXX: If failure, revert the insertion
1743 switch(src_cap->type) {
1744 case ObjType_EndPoint:
1745 // XXX: FIXME: check that buffer offset lies wholly within the disp frame
1746 // can't easily enforce this here, because the dispatcher frame may not
1748 /* if (param1 < sizeof(struct dispatcher) ||
1749 dest_cap->u.endpoint.listener->disp == NULL ||
1750 param2 < IDC_RECV_LENGTH ||
1751 param1 + sizeof(struct idc_endpoint) + param2 * sizeof(uintptr_t) >
1752 (1UL << dest_cap->u.endpoint.listener->disp_cte.cap.u.frame.bits)) {
1753 return SYS_ERR_INVALID_EPBUF;
1755 if (param2 < LMP_RECV_HEADER_LENGTH) {
1756 return SYS_ERR_INVALID_EPLEN;
1758 dest_cap->u.endpoint.epoffset = param1;
1759 dest_cap->u.endpoint.epbuflen = param2;
1763 if(src_cap->u.io.start <= param1) {
1764 dest_cap->u.io.start = param1;
1766 if(src_cap->u.io.end >= param2) {
1767 dest_cap->u.io.end = param2;
1772 // Unhandled source type for mint
1773 return SYS_ERR_INVALID_SOURCE_TYPE;
1776 // Insert after doing minting operation
1777 mdb_insert(dest_cte);