| 1 | /*
|
|---|
| 2 | * Copyright (c) 2005 Jakub Jermar
|
|---|
| 3 | * Copyright (c) 2005 Sergey Bondari
|
|---|
| 4 | * All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 7 | * modification, are permitted provided that the following conditions
|
|---|
| 8 | * are met:
|
|---|
| 9 | *
|
|---|
| 10 | * - Redistributions of source code must retain the above copyright
|
|---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 14 | * documentation and/or other materials provided with the distribution.
|
|---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 16 | * derived from this software without specific prior written permission.
|
|---|
| 17 | *
|
|---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| 30 | /** @addtogroup genericmm
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 | /** @file
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #ifndef KERN_FRAME_H_
|
|---|
| 37 | #define KERN_FRAME_H_
|
|---|
| 38 |
|
|---|
| 39 | #include <typedefs.h>
|
|---|
| 40 | #include <trace.h>
|
|---|
| 41 | #include <adt/list.h>
|
|---|
| 42 | #include <mm/buddy.h>
|
|---|
| 43 | #include <synch/spinlock.h>
|
|---|
| 44 | #include <arch/mm/page.h>
|
|---|
| 45 | #include <arch/mm/frame.h>
|
|---|
| 46 |
|
|---|
| 47 | #define ONE_FRAME 0
|
|---|
| 48 | #define TWO_FRAMES 1
|
|---|
| 49 | #define FOUR_FRAMES 2
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 | #ifdef ARCH_STACK_FRAMES
|
|---|
| 53 | #define STACK_FRAMES ARCH_STACK_FRAMES
|
|---|
| 54 | #else
|
|---|
| 55 | #define STACK_FRAMES ONE_FRAME
|
|---|
| 56 | #endif
|
|---|
| 57 |
|
|---|
| 58 | /** Maximum number of zones in the system. */
|
|---|
| 59 | #define ZONES_MAX 32
|
|---|
| 60 |
|
|---|
| 61 | typedef uint8_t frame_flags_t;
|
|---|
| 62 |
|
|---|
| 63 | /** Convert the frame address to kernel VA. */
|
|---|
| 64 | #define FRAME_KA 0x01
|
|---|
| 65 | /** Do not panic and do not sleep on failure. */
|
|---|
| 66 | #define FRAME_ATOMIC 0x02
|
|---|
| 67 | /** Do not start reclaiming when no free memory. */
|
|---|
| 68 | #define FRAME_NO_RECLAIM 0x04
|
|---|
| 69 |
|
|---|
| 70 | typedef uint8_t zone_flags_t;
|
|---|
| 71 |
|
|---|
| 72 | /** Available zone (free for allocation) */
|
|---|
| 73 | #define ZONE_AVAILABLE 0x00
|
|---|
| 74 | /** Zone is reserved (not available for allocation) */
|
|---|
| 75 | #define ZONE_RESERVED 0x08
|
|---|
| 76 | /** Zone is used by firmware (not available for allocation) */
|
|---|
| 77 | #define ZONE_FIRMWARE 0x10
|
|---|
| 78 |
|
|---|
| 79 | /** Currently there is no equivalent zone flags
|
|---|
| 80 | for frame flags */
|
|---|
| 81 | #define FRAME_TO_ZONE_FLAGS(frame_flags) 0
|
|---|
| 82 |
|
|---|
| 83 | typedef struct {
|
|---|
| 84 | size_t refcount; /**< Tracking of shared frames */
|
|---|
| 85 | uint8_t buddy_order; /**< Buddy system block order */
|
|---|
| 86 | link_t buddy_link; /**< Link to the next free block inside
|
|---|
| 87 | one order */
|
|---|
| 88 | void *parent; /**< If allocated by slab, this points there */
|
|---|
| 89 | } frame_t;
|
|---|
| 90 |
|
|---|
| 91 | typedef struct {
|
|---|
| 92 | pfn_t base; /**< Frame_no of the first frame
|
|---|
| 93 | in the frames array */
|
|---|
| 94 | size_t count; /**< Size of zone */
|
|---|
| 95 | size_t free_count; /**< Number of free frame_t
|
|---|
| 96 | structures */
|
|---|
| 97 | size_t busy_count; /**< Number of busy frame_t
|
|---|
| 98 | structures */
|
|---|
| 99 | zone_flags_t flags; /**< Type of the zone */
|
|---|
| 100 |
|
|---|
| 101 | frame_t *frames; /**< Array of frame_t structures
|
|---|
| 102 | in this zone */
|
|---|
| 103 | buddy_system_t *buddy_system; /**< Buddy system for the zone */
|
|---|
| 104 | } zone_t;
|
|---|
| 105 |
|
|---|
| 106 | /*
|
|---|
| 107 | * The zoneinfo.lock must be locked when accessing zoneinfo structure.
|
|---|
| 108 | * Some of the attributes in zone_t structures are 'read-only'
|
|---|
| 109 | */
|
|---|
| 110 | typedef struct {
|
|---|
| 111 | IRQ_SPINLOCK_DECLARE(lock);
|
|---|
| 112 | size_t count;
|
|---|
| 113 | zone_t info[ZONES_MAX];
|
|---|
| 114 | } zones_t;
|
|---|
| 115 |
|
|---|
| 116 | extern zones_t zones;
|
|---|
| 117 |
|
|---|
| 118 | NO_TRACE static inline uintptr_t PFN2ADDR(pfn_t frame)
|
|---|
| 119 | {
|
|---|
| 120 | return (uintptr_t) (frame << FRAME_WIDTH);
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | NO_TRACE static inline pfn_t ADDR2PFN(uintptr_t addr)
|
|---|
| 124 | {
|
|---|
| 125 | return (pfn_t) (addr >> FRAME_WIDTH);
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | NO_TRACE static inline size_t SIZE2FRAMES(size_t size)
|
|---|
| 129 | {
|
|---|
| 130 | if (!size)
|
|---|
| 131 | return 0;
|
|---|
| 132 | return (size_t) ((size - 1) >> FRAME_WIDTH) + 1;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | NO_TRACE static inline size_t FRAMES2SIZE(size_t frames)
|
|---|
| 136 | {
|
|---|
| 137 | return (size_t) (frames << FRAME_WIDTH);
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | NO_TRACE static inline bool zone_flags_available(zone_flags_t flags)
|
|---|
| 141 | {
|
|---|
| 142 | return ((flags & (ZONE_RESERVED | ZONE_FIRMWARE)) == 0);
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | #define IS_BUDDY_ORDER_OK(index, order) \
|
|---|
| 146 | ((~(((unative_t) -1) << (order)) & (index)) == 0)
|
|---|
| 147 | #define IS_BUDDY_LEFT_BLOCK(zone, frame) \
|
|---|
| 148 | (((frame_index((zone), (frame)) >> (frame)->buddy_order) & 0x01) == 0)
|
|---|
| 149 | #define IS_BUDDY_RIGHT_BLOCK(zone, frame) \
|
|---|
| 150 | (((frame_index((zone), (frame)) >> (frame)->buddy_order) & 0x01) == 1)
|
|---|
| 151 | #define IS_BUDDY_LEFT_BLOCK_ABS(zone, frame) \
|
|---|
| 152 | (((frame_index_abs((zone), (frame)) >> (frame)->buddy_order) & 0x01) == 0)
|
|---|
| 153 | #define IS_BUDDY_RIGHT_BLOCK_ABS(zone, frame) \
|
|---|
| 154 | (((frame_index_abs((zone), (frame)) >> (frame)->buddy_order) & 0x01) == 1)
|
|---|
| 155 |
|
|---|
| 156 | #define frame_alloc(order, flags) \
|
|---|
| 157 | frame_alloc_generic(order, flags, NULL)
|
|---|
| 158 |
|
|---|
| 159 | extern void frame_init(void);
|
|---|
| 160 | extern void *frame_alloc_generic(uint8_t, frame_flags_t, size_t *);
|
|---|
| 161 | extern void frame_free(uintptr_t);
|
|---|
| 162 | extern void frame_reference_add(pfn_t);
|
|---|
| 163 |
|
|---|
| 164 | extern size_t find_zone(pfn_t frame, size_t count, size_t hint);
|
|---|
| 165 | extern size_t zone_create(pfn_t, size_t, pfn_t, zone_flags_t);
|
|---|
| 166 | extern void *frame_get_parent(pfn_t, size_t);
|
|---|
| 167 | extern void frame_set_parent(pfn_t, void *, size_t);
|
|---|
| 168 | extern void frame_mark_unavailable(pfn_t, size_t);
|
|---|
| 169 | extern size_t zone_conf_size(size_t);
|
|---|
| 170 | extern bool zone_merge(size_t, size_t);
|
|---|
| 171 | extern void zone_merge_all(void);
|
|---|
| 172 | extern uint64_t zones_total_size(void);
|
|---|
| 173 | extern void zones_stats(uint64_t *, uint64_t *, uint64_t *, uint64_t *);
|
|---|
| 174 |
|
|---|
| 175 | /*
|
|---|
| 176 | * Console functions
|
|---|
| 177 | */
|
|---|
| 178 | extern void zones_print_list(void);
|
|---|
| 179 | extern void zone_print_one(size_t);
|
|---|
| 180 |
|
|---|
| 181 | #endif
|
|---|
| 182 |
|
|---|
| 183 | /** @}
|
|---|
| 184 | */
|
|---|