source: rtems-libbsd/rtemsbsd/rtems/rtems-kernel-page.c @ 5b1f20b

55-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 5b1f20b was 5b1f20b, checked in by Sebastian Huber <sebastian.huber@…>, on 05/06/16 at 19:22:38

Rename files for kernel namespace script

This makes it easier to create the kernel namespace header.

  • Property mode set to 100644
File size: 3.9 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup rtems_bsd_rtems
5 *
6 * @brief TODO.
7 */
8
9/*
10 * Copyright (c) 2015 embedded brains GmbH.  All rights reserved.
11 *
12 *  embedded brains GmbH
13 *  Dornierstr. 4
14 *  82178 Puchheim
15 *  Germany
16 *  <rtems@embedded-brains.de>
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 *    notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 *    notice, this list of conditions and the following disclaimer in the
25 *    documentation and/or other materials provided with the distribution.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 */
39
40#include <machine/rtems-bsd-kernel-space.h>
41#include <machine/rtems-bsd-page.h>
42
43#include <rtems/bsd/sys/param.h>
44#include <rtems/bsd/sys/types.h>
45#include <rtems/bsd/sys/lock.h>
46#include <sys/mutex.h>
47#include <sys/systm.h>
48#include <sys/kernel.h>
49#include <vm/uma.h>
50
51#include <stdlib.h>
52
53#include <rtems/bsd/bsd.h>
54#include <rtems/malloc.h>
55#include <rtems/rbheap.h>
56
57void **rtems_bsd_page_object_table;
58
59uintptr_t rtems_bsd_page_area_begin;
60
61static rtems_rbheap_control page_heap;
62
63struct mtx page_heap_mtx;
64
65void *
66rtems_bsd_page_alloc(uintptr_t size_in_bytes, int wait)
67{
68        void *addr;
69
70        mtx_lock(&page_heap_mtx);
71
72        addr = rtems_rbheap_allocate(&page_heap, size_in_bytes);
73        if (addr == NULL && wait) {
74                int i;
75
76                for (i = 0; i < 8; i++) {
77                        mtx_unlock(&page_heap_mtx);
78                        uma_reclaim();
79                        mtx_lock(&page_heap_mtx);
80
81                        addr = rtems_rbheap_allocate(&page_heap, size_in_bytes);
82                        if (addr != NULL)
83                                break;
84
85                        msleep(&page_heap, &page_heap_mtx, 0,
86                            "page alloc", (hz / 4) * (i + 1));
87                }
88
89                if (i == 8) {
90                        panic("rtems_bsd_page_alloc: page starvation");
91                }
92        }
93
94        mtx_unlock(&page_heap_mtx);
95
96#ifdef INVARIANTS
97        if (addr != NULL) {
98                memset(addr, 0, size_in_bytes);
99        }
100#endif
101
102        return (addr);
103}
104
105void
106rtems_bsd_page_free(void *addr)
107{
108        mtx_lock(&page_heap_mtx);
109        rtems_rbheap_free(&page_heap, addr);
110        wakeup(&page_heap);
111        mtx_unlock(&page_heap_mtx);
112}
113
114static void
115rtems_bsd_page_init(void *arg)
116{
117        rtems_status_code sc;
118        void *area;
119        void **obj_table;
120        rtems_rbheap_chunk *chunks;
121        size_t i;
122        size_t n;
123        uintptr_t heap_size;
124
125        mtx_init(&page_heap_mtx, "page heap", NULL, MTX_DEF);
126
127        heap_size = rtems_bsd_get_allocator_domain_size(
128            RTEMS_BSD_ALLOCATOR_DOMAIN_PAGE);
129
130        area = rtems_heap_allocate_aligned_with_boundary(heap_size, PAGE_SIZE,
131            0);
132        BSD_ASSERT(area != NULL);
133
134        sc = rtems_rbheap_initialize(&page_heap, area, heap_size, PAGE_SIZE,
135            rtems_rbheap_extend_descriptors_with_malloc, NULL);
136        BSD_ASSERT(sc == RTEMS_SUCCESSFUL);
137
138        rtems_rbheap_set_extend_descriptors(&page_heap,
139            rtems_rbheap_extend_descriptors_never);
140
141        n = heap_size / PAGE_SIZE;
142
143        chunks = malloc(n * sizeof(*chunks), M_RTEMS_HEAP, M_NOWAIT);
144        BSD_ASSERT(chunks != NULL);
145
146        for (i = 0; i < n; ++i) {
147                rtems_rbheap_add_to_spare_descriptor_chain(&page_heap,
148                    &chunks[i]);
149        }
150
151        obj_table = calloc(n, sizeof(*obj_table));
152
153        rtems_bsd_page_area_begin = (uintptr_t)area;
154        rtems_bsd_page_object_table = obj_table;
155}
156
157SYSINIT(rtems_bsd_page, SI_SUB_VM, SI_ORDER_FIRST, rtems_bsd_page_init, NULL);
Note: See TracBrowser for help on using the repository browser.