source: rtems-libbsd/rtemsbsd/rtems/rtems-kernel-page.c @ 7533adc

55-freebsd-126-freebsd-12
Last change on this file since 7533adc was 7533adc, checked in by Sebastian Huber <sebastian.huber@…>, on 11/10/16 at 13:58:22

rtems-kernel-page: Add used pages counter

  • Property mode set to 100644
File size: 4.1 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 <sys/param.h>
44#include <sys/types.h>
45#include <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 struct {
62        struct mtx mtx;
63        rtems_rbheap_control heap;
64        size_t used;
65} page_alloc;
66
67void *
68rtems_bsd_page_alloc(uintptr_t size_in_bytes, int wait)
69{
70        void *addr;
71
72        mtx_lock(&page_alloc.mtx);
73
74        addr = rtems_rbheap_allocate(&page_alloc.heap, size_in_bytes);
75        if (addr == NULL && wait) {
76                int i;
77
78                for (i = 0; i < 8; i++) {
79                        mtx_unlock(&page_alloc.mtx);
80                        uma_reclaim();
81                        mtx_lock(&page_alloc.mtx);
82
83                        addr = rtems_rbheap_allocate(&page_alloc.heap,
84                            size_in_bytes);
85                        if (addr != NULL)
86                                break;
87
88                        msleep(&page_alloc.heap, &page_alloc.mtx, 0,
89                            "page alloc", (hz / 4) * (i + 1));
90                }
91
92                if (i == 8) {
93                        panic("rtems_bsd_page_alloc: page starvation");
94                }
95        }
96
97        page_alloc.used += (addr != NULL) ? 1 : 0;
98        mtx_unlock(&page_alloc.mtx);
99
100#ifdef INVARIANTS
101        wait |= M_ZERO;
102#endif
103
104        if (addr != NULL && (wait & M_ZERO) != 0) {
105                memset(addr, 0, size_in_bytes);
106        }
107
108        return (addr);
109}
110
111void
112rtems_bsd_page_free(void *addr)
113{
114
115        mtx_lock(&page_alloc.mtx);
116        --page_alloc.used;
117        rtems_rbheap_free(&page_alloc.heap, addr);
118        wakeup(&page_alloc.heap);
119        mtx_unlock(&page_alloc.mtx);
120}
121
122static void
123rtems_bsd_page_init(void *arg)
124{
125        rtems_status_code sc;
126        void *area;
127        void **obj_table;
128        rtems_rbheap_chunk *chunks;
129        size_t i;
130        size_t n;
131        uintptr_t heap_size;
132
133        mtx_init(&page_alloc.mtx, "page heap", NULL, MTX_DEF);
134
135        heap_size = rtems_bsd_get_allocator_domain_size(
136            RTEMS_BSD_ALLOCATOR_DOMAIN_PAGE);
137
138        area = rtems_heap_allocate_aligned_with_boundary(heap_size, PAGE_SIZE,
139            0);
140        BSD_ASSERT(area != NULL);
141
142        sc = rtems_rbheap_initialize(&page_alloc.heap, area, heap_size,
143            PAGE_SIZE, rtems_rbheap_extend_descriptors_with_malloc, NULL);
144        BSD_ASSERT(sc == RTEMS_SUCCESSFUL);
145
146        rtems_rbheap_set_extend_descriptors(&page_alloc.heap,
147            rtems_rbheap_extend_descriptors_never);
148
149        n = heap_size / PAGE_SIZE;
150
151        chunks = malloc(n * sizeof(*chunks), M_RTEMS_HEAP, M_NOWAIT);
152        BSD_ASSERT(chunks != NULL);
153
154        for (i = 0; i < n; ++i) {
155                rtems_rbheap_add_to_spare_descriptor_chain(&page_alloc.heap,
156                    &chunks[i]);
157        }
158
159        obj_table = calloc(n, sizeof(*obj_table));
160
161        rtems_bsd_page_area_begin = (uintptr_t)area;
162        rtems_bsd_page_object_table = obj_table;
163}
164
165SYSINIT(rtems_bsd_page, SI_SUB_VM, SI_ORDER_FIRST, rtems_bsd_page_init, NULL);
Note: See TracBrowser for help on using the repository browser.