blob: 15924b18d3894061fbf4e7ff2c3d425b21600861 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (c) 2015 Google, Inc. All rights reserved
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24#include <lib/page_alloc.h>
25
26#include <debug.h>
27#include <assert.h>
28#include <string.h>
29#include <trace.h>
30#if WITH_KERNEL_VM
31#include <kernel/vm.h>
32#else
33#include <kernel/novm.h>
34#endif
35
36/* A simple page-aligned wrapper around the pmm or novm implementation of
37 * the underlying physical page allocator. Used by system heaps or any
38 * other user that wants pages of memory but doesn't want to use LK
39 * specific apis.
40 */
41#define LOCAL_TRACE 0
42
43#if WITH_STATIC_HEAP
44
45#error "fix static heap post page allocator and novm stuff"
46
47#if !defined(HEAP_START) || !defined(HEAP_LEN)
48#error WITH_STATIC_HEAP set but no HEAP_START or HEAP_LEN defined
49#endif
50
51#endif
52
53void *page_alloc(size_t pages) {
54#if WITH_KERNEL_VM
55 void *result = pmm_alloc_kpages(pages, NULL);
56 return result;
57#else
58 void *result = novm_alloc_pages(pages, NOVM_ARENA_ANY);
59 return result;
60#endif
61}
62
63void page_free(void *ptr, size_t pages) {
64#if WITH_KERNEL_VM
65 DEBUG_ASSERT(IS_PAGE_ALIGNED((uintptr_t)ptr));
66
67 pmm_free_kpages(ptr, pages);
68#else
69 novm_free_pages(ptr, pages);
70#endif
71}
72
73void *page_first_alloc(size_t *size_return) {
74#if WITH_KERNEL_VM
75 *size_return = PAGE_SIZE;
76 return page_alloc(1);
77#else
78 return novm_alloc_unaligned(size_return);
79#endif
80}
81
82#if LK_DEBUGLEVEL > 1
83#if WITH_LIB_CONSOLE
84
85#include <lib/console.h>
86
87static int cmd_page_alloc(int argc, const cmd_args *argv);
88static void page_alloc_dump(void);
89
90STATIC_COMMAND_START
91STATIC_COMMAND("page_alloc", "page allocator debug commands", &cmd_page_alloc)
92STATIC_COMMAND_END(page_alloc);
93
94static int cmd_page_alloc(int argc, const cmd_args *argv)
95{
96 if (argc != 2) {
97notenoughargs:
98 printf("not enough arguments\n");
99usage:
100 printf("usage:\n");
101 printf("\t%s info\n", argv[0].str);
102 return -1;
103 }
104
105 if (strcmp(argv[1].str, "info") == 0) {
106 page_alloc_dump();
107 } else {
108 printf("unrecognized command\n");
109 goto usage;
110 }
111
112 return 0;
113}
114
115static void page_alloc_dump(void)
116{
117#ifdef WITH_KERNEL_VM
118 dprintf(INFO, "Page allocator is based on pmm\n");
119#else
120 dprintf(INFO, "Page allocator is based on novm\n");
121#endif
122}
123
124#endif
125#endif