1*dc534cd0Sjmc.\" $OpenBSD: km_alloc.9,v 1.9 2019/12/06 19:15:16 jmc Exp $ 260e67ac3Sart.\" Copyright (c) 2011 Artur Grabowski <art@openbsd.org> 360e67ac3Sart.\" All rights reserved. 460e67ac3Sart.\" 560e67ac3Sart.\" Permission to use, copy, modify, and distribute this software for any 660e67ac3Sart.\" purpose with or without fee is hereby granted, provided that the above 760e67ac3Sart.\" copyright notice and this permission notice appear in all copies. 860e67ac3Sart.\" 960e67ac3Sart.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 1060e67ac3Sart.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 1160e67ac3Sart.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 1260e67ac3Sart.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 1360e67ac3Sart.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 1460e67ac3Sart.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 1560e67ac3Sart.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 1660e67ac3Sart.\" 17*dc534cd0Sjmc.Dd $Mdocdate: December 6 2019 $ 1860e67ac3Sart.Dt KM_ALLOC 9 1960e67ac3Sart.Os 2060e67ac3Sart.Sh NAME 2160e67ac3Sart.Nm km_alloc , 2260e67ac3Sart.Nm km_free 2360e67ac3Sart.Nd kernel memory allocator 2460e67ac3Sart.Sh SYNOPSIS 25dddd2645Sschwarze.In sys/types.h 26dddd2645Sschwarze.In uvm/uvm_extern.h 2760e67ac3Sart.Ft void * 28f7b1c266Smlarkin.Fn km_alloc "size_t sz" "const struct kmem_va_mode *kv" "const struct kmem_pa_mode *kp" "const struct kmem_dyn_mode *kd" 2960e67ac3Sart.Ft void 30f7b1c266Smlarkin.Fn km_free "void *v" "size_t sz" "const struct kmem_va_mode *kv" "const struct kmem_pa_mode *kp" 3160e67ac3Sart.Sh DESCRIPTION 3260e67ac3SartThe 3360e67ac3Sart.Fn km_alloc 3460e67ac3Sartfunction allocates kernel virtual space optionally backed by physical pages. 3560e67ac3SartThe 3660e67ac3Sart.Fn km_free 3760e67ac3Sartfunction frees the space that was previously allocated by 3860e67ac3Sart.Fn km_alloc . 3960e67ac3Sart.Pp 4060e67ac3SartThe 41f7b1c266Smlarkin.Fa sz 4260e67ac3Sartargument specifies the size of the allocation and must be a multiple 4360e67ac3Sartof 4460e67ac3Sart.Dv PAGE_SIZE . 4560e67ac3SartThe 4660e67ac3Sart.Fa kv 4760e67ac3Sartand 4860e67ac3Sart.Fa kp 49ab93d6c0Sjmcarguments specify the type of virtual and physical memory to be allocated. 5060e67ac3SartThe 5160e67ac3Sart.Fa kd 5260e67ac3Sartargument specifies additional options for the allocation. 5360e67ac3SartThe arguments passed to 5460e67ac3Sart.Fn km_free 5560e67ac3Sartmust match those that were used to obtain the space in 56f4ceec41Sart.Fn km_alloc . 5760e67ac3Sart.Pp 5860e67ac3SartTypically a user will use certain predefined modes for memory allocation. 5960e67ac3SartFor virtual space the predefined modes are: 60ab93d6c0Sjmc.Pp 61ab93d6c0Sjmc.Bl -tag -width kv_intrsafe -offset 3n -compact 6260e67ac3Sart.It kv_any 6360e67ac3SartAllocates the virtual space anywhere. 6460e67ac3Sart.It kv_intrsafe 6560e67ac3SartAllocates the virtual space in the interrupt safe map. 6660e67ac3Sart.It kv_page 6760e67ac3SartAllocates single pages. 6860e67ac3Sart.El 6960e67ac3Sart.Pp 7060e67ac3SartFor physical pages the predefined modes are: 71ab93d6c0Sjmc.Pp 72ab93d6c0Sjmc.Bl -tag -width kp_pageable -offset 3n -compact 7360e67ac3Sart.It kp_dirty 7460e67ac3SartMaps dirty pages into the allocation. 7560e67ac3Sart.It kp_zero 7660e67ac3SartMaps zeroed pages into the allocation. 7760e67ac3Sart.It kp_dma 7860e67ac3SartMaps dma-accessible pages into the allocation. 7960e67ac3Sart.It kp_dma_zero 8060e67ac3SartMaps zeroed dma-accessible pages into the allocation. 8160e67ac3Sart.It kp_pageable 8260e67ac3SartPages will be demand paged. 8360e67ac3Sart.It kp_none 8460e67ac3SartLeaves the allocation unmapped. 8560e67ac3Sart.El 8660e67ac3Sart.Pp 8760e67ac3SartThe other parameters for allocation are: 88ab93d6c0Sjmc.Pp 89ab93d6c0Sjmc.Bl -tag -width kd_trylock -offset 3n -compact 9060e67ac3Sart.It kd_waitok 9160e67ac3SartSleeping for physical pages is allowed. 9260e67ac3Sart.It kd_nowait 9360e67ac3SartSleeping is not allowed. 9460e67ac3Sart.It kd_trylock 9560e67ac3SartFail if the allocator cannot obtain locks without waiting. 9660e67ac3Sart.El 9760e67ac3Sart.Pp 9860e67ac3SartIn case the predefined allocation modes are not sufficient, a custom allocation 9960e67ac3Sartmode can be created. 1003967d4f7SsobradoThe structure controlling the virtual space allocation is: 10160e67ac3Sart.Bd -literal 10260e67ac3Sartstruct kmem_va_mode { 10360e67ac3Sart struct vm_map **kv_map; 10460e67ac3Sart vsize_t kv_align; 10560e67ac3Sart int kv_wait; 10660e67ac3Sart int kv_singlepage; 10760e67ac3Sart}; 10860e67ac3Sart.Ed 10960e67ac3Sart.Bl -tag -width kv_singlepage 11060e67ac3Sart.It kv_map 11160e67ac3SartA pointer to the pointer to the uvm_map the space will be allocated from. 11260e67ac3Sart.It kv_align 11360e67ac3SartAlignment constraint of the allocated space. 11460e67ac3Sart.It kv_wait 11560e67ac3SartA flag indicating whether the allocator should wait for space to be freed if 11660e67ac3Sartthe allocation cannot be satisfied. 11760e67ac3Sart.It kv_singlepage 11860e67ac3SartA flag indicating if the allocations will always be for single pages. 11960e67ac3Sart.El 12060e67ac3Sart.Bd -literal 12160e67ac3Sartstruct kmem_pa_mode { 12260e67ac3Sart struct uvm_constraint_range *kp_constraint; 12360e67ac3Sart struct uvm_object **kp_object; 12460e67ac3Sart paddr_t kp_align; 12560e67ac3Sart paddr_t kp_boundary; 12660e67ac3Sart int kp_nomem; 12760e67ac3Sart int kp_maxseg; 12860e67ac3Sart int kp_zero; 12960e67ac3Sart int kp_pageable; 13060e67ac3Sart}; 13160e67ac3Sart.Ed 13260e67ac3Sart.Bl -tag -width kp_constraint 13360e67ac3Sart.It kp_constraint 13460e67ac3SartA pointer to physical allocation constraints. 13560e67ac3Sart.It kp_object 13660e67ac3SartA pointer to a pointer to a uvm_object if the pages should be backed by a 13760e67ac3Sartkernel object. 13860e67ac3Sart.It kp_align 13960e67ac3SartPhysical alignment of the first page in the allocation. 14060e67ac3Sart.It kp_boundary 14160e67ac3SartBoundary that the physical addresses can't cross if the allocation is 14260e67ac3Sartcontiguous. 14360e67ac3Sart.It kp_nomem 14460e67ac3SartA flag that specifies that the allocation should not be backed by physical 14560e67ac3Sartpages. 14660e67ac3Sart.It kp_maxseg 14760e67ac3SartMaximal amount of contiguous physical segments in the allocation. 14860e67ac3Sart.It kp_zero 14960e67ac3SartA flag that specifies if the returned memory should be zeroed. 15060e67ac3Sart.It kp_pageable 15160e67ac3SartA flag that specifies if the returned memory should be demand paged from the 15260e67ac3Sartbacking object instead of being allocated up front. 15360e67ac3Sart.El 15460e67ac3Sart.Bd -literal 15560e67ac3Sartstruct kmem_dyn_mode { 15660e67ac3Sart int kd_waitok; 15760e67ac3Sart int kd_trylock; 15860e67ac3Sart voff_t kd_prefer; 15960e67ac3Sart int *kd_slowdown; 16060e67ac3Sart}; 16160e67ac3Sart.Ed 16260e67ac3Sart.Bl -tag -width kd_slowdown 16360e67ac3Sart.It kd_waitok 16460e67ac3SartA flag that specifies if the allocator may sleep waiting for memory. 16560e67ac3Sart.It kd_trylock 16660e67ac3SartA flag that specifies if the allocator should fail if it can't immediately 16760e67ac3Sartobtain a lock. 16860e67ac3Sart.It kd_prefer 16960e67ac3SartAn offset given to PMAP_PREFER to virtually align the allocated space. 17060e67ac3Sart.It kd_slowdown 17160e67ac3SartA pointer to an integer that will be set to 1 if the internal single page 17260e67ac3Sartallocator needs the caller to back off to allow the allocator to catch up. 17360e67ac3Sart.El 17460e67ac3Sart.Sh RETURN VALUES 17560e67ac3Sart.Fn km_alloc 17660e67ac3Sartreturns a kernel virtual address or 17760e67ac3Sart.Dv NULL 1783967d4f7Ssobradoif the allocation cannot be satisfied. 17960e67ac3Sart.Sh SEE ALSO 18060e67ac3Sart.Xr malloc 9 , 181*dc534cd0Sjmc.Xr uvm_init 9 182