source: rtems-libbsd/rtemsbsd/rtems/rtems-bsd-page.c @ 3c302b6

4.115-freebsd-12freebsd-9.3
Last change on this file since 3c302b6 was 3c302b6, checked in by Sebastian Huber <sebastian.huber@…>, on Jan 27, 2015 at 7:26:53 AM

Add a simple page allocator

  • Property mode set to 100644
File size: 3.8 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/malloc.h>
54#include <rtems/rbheap.h>
55
56/* FIXME: This must be application configurable */
57#define PAGE_HEAP_SIZE (8 * 1024 * 1024)
58
59void **rtems_bsd_page_object_table;
60
61uintptr_t rtems_bsd_page_area_begin;
62
63static rtems_rbheap_control page_heap;
64
65struct mtx page_heap_mtx;
66
67void *
68rtems_bsd_page_alloc(uintptr_t size_in_bytes, int wait)
69{
70        void *addr;
71
72        mtx_lock(&page_heap_mtx);
73
74        addr = rtems_rbheap_allocate(&page_heap, size_in_bytes);
75        if (addr == NULL && wait) {
76                int i;
77
78                for (i = 0; i < 8; i++) {
79                        mtx_unlock(&page_heap_mtx);
80                        uma_reclaim();
81                        mtx_lock(&page_heap_mtx);
82
83                        addr = rtems_rbheap_allocate(&page_heap, size_in_bytes);
84                        if (addr != NULL)
85                                break;
86
87                        msleep(&page_heap, &page_heap_mtx, 0,
88                            "page alloc", (hz / 4) * (i + 1));
89                }
90
91                if (i == 8) {
92                        panic("rtems_bsd_page_alloc: page starvation");
93                }
94        }
95
96        mtx_unlock(&page_heap_mtx);
97
98        return (addr);
99}
100
101void
102rtems_bsd_page_free(void *addr)
103{
104        mtx_lock(&page_heap_mtx);
105        rtems_rbheap_free(&page_heap, addr);
106        wakeup(&page_heap);
107        mtx_unlock(&page_heap_mtx);
108}
109
110static void
111rtems_bsd_page_init(void *arg)
112{
113        rtems_status_code sc;
114        void *area;
115        void **obj_table;
116        rtems_rbheap_chunk *chunks;
117        size_t i;
118        size_t n;
119
120        mtx_init(&page_heap_mtx, "page heap", NULL, MTX_DEF);
121
122        area = rtems_heap_allocate_aligned_with_boundary(PAGE_HEAP_SIZE,
123            PAGE_SIZE, 0);
124        BSD_ASSERT(area != NULL);
125
126        sc = rtems_rbheap_initialize(&page_heap, area, PAGE_HEAP_SIZE,
127            PAGE_SIZE, rtems_rbheap_extend_descriptors_with_malloc, NULL);
128        BSD_ASSERT(sc == RTEMS_SUCCESSFUL);
129
130        rtems_rbheap_set_extend_descriptors(&page_heap,
131            rtems_rbheap_extend_descriptors_never);
132
133        n = PAGE_HEAP_SIZE / PAGE_SIZE;
134
135        chunks = malloc(n * sizeof(*chunks), M_RTEMS_HEAP, M_NOWAIT);
136        BSD_ASSERT(chunks != NULL);
137
138        for (i = 0; i < n; ++i) {
139                rtems_rbheap_add_to_spare_descriptor_chain(&page_heap,
140                    &chunks[i]);
141        }
142
143        obj_table = calloc(n, sizeof(*obj_table));
144
145        rtems_bsd_page_area_begin = (uintptr_t)area;
146        rtems_bsd_page_object_table = obj_table;
147}
148
149SYSINIT(rtems_bsd_page, SI_SUB_VM, SI_ORDER_FIRST, rtems_bsd_page_init, NULL);
Note: See TracBrowser for help on using the repository browser.