Rev 724 | Rev 762 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | jermar | 1 | /* |
2 | * Copyright (C) 2005 Jakub Jermar |
||
480 | bondari | 3 | * Copyright (C) 2005 Sergey Bondari |
1 | jermar | 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 | #ifndef __FRAME_H__ |
||
31 | #define __FRAME_H__ |
||
32 | |||
33 | #include <arch/types.h> |
||
102 | jermar | 34 | #include <typedefs.h> |
354 | jermar | 35 | #include <list.h> |
36 | #include <synch/spinlock.h> |
||
479 | bondari | 37 | #include <mm/buddy.h> |
759 | palkovsky | 38 | #include <mm/slab.h> |
1 | jermar | 39 | |
548 | jermar | 40 | #define ONE_FRAME 0 |
41 | |||
689 | bondari | 42 | #define FRAME_KA 1 /* skip frames conflicting with user address space */ |
43 | #define FRAME_PANIC 2 /* panic on failure */ |
||
759 | palkovsky | 44 | #define FRAME_ATOMIC 4 /* do not panic and do not sleep on failure */ |
1 | jermar | 45 | |
689 | bondari | 46 | #define FRAME_OK 0 /* frame_alloc return status */ |
47 | #define FRAME_NO_MEMORY 1 /* frame_alloc return status */ |
||
48 | #define FRAME_ERROR 2 /* frame_alloc return status */ |
||
49 | |||
480 | bondari | 50 | #define FRAME2ADDR(zone, frame) ((zone)->base + ((frame) - (zone)->frames) * FRAME_SIZE) |
51 | #define ADDR2FRAME(zone, addr) (&((zone)->frames[((addr) - (zone)->base) / FRAME_SIZE])) |
||
538 | jermar | 52 | #define FRAME_INDEX(zone, frame) ((index_t)((frame) - (zone)->frames)) |
724 | palkovsky | 53 | #define FRAME_INDEX_ABS(zone, frame) (((index_t)((frame) - (zone)->frames)) + (zone)->base_index) |
533 | bondari | 54 | #define FRAME_INDEX_VALID(zone, index) (((index) >= 0) && ((index) < ((zone)->free_count + (zone)->busy_count))) |
564 | jermar | 55 | #define IS_BUDDY_ORDER_OK(index, order) ((~(((__native) -1) << (order)) & (index)) == 0) |
56 | #define IS_BUDDY_LEFT_BLOCK(zone, frame) (((FRAME_INDEX((zone), (frame)) >> (frame)->buddy_order) & 0x1) == 0) |
||
57 | #define IS_BUDDY_RIGHT_BLOCK(zone, frame) (((FRAME_INDEX((zone), (frame)) >> (frame)->buddy_order) & 0x1) == 1) |
||
724 | palkovsky | 58 | #define IS_BUDDY_LEFT_BLOCK_ABS(zone, frame) (((FRAME_INDEX_ABS((zone), (frame)) >> (frame)->buddy_order) & 0x1) == 0) |
59 | #define IS_BUDDY_RIGHT_BLOCK_ABS(zone, frame) (((FRAME_INDEX_ABS((zone), (frame)) >> (frame)->buddy_order) & 0x1) == 1) |
||
479 | bondari | 60 | |
629 | decky | 61 | #define ZONE_BLACKLIST_SIZE 4 |
480 | bondari | 62 | |
366 | jermar | 63 | struct zone { |
64 | link_t link; /**< link to previous and next zone */ |
||
354 | jermar | 65 | |
623 | jermar | 66 | SPINLOCK_DECLARE(lock); /**< this lock protects everything below */ |
355 | jermar | 67 | __address base; /**< physical address of the first frame in the frames array */ |
724 | palkovsky | 68 | index_t base_index; /**< frame index offset of the zone base */ |
355 | jermar | 69 | frame_t *frames; /**< array of frame_t structures in this zone */ |
539 | jermar | 70 | count_t free_count; /**< number of free frame_t structures */ |
71 | count_t busy_count; /**< number of busy frame_t structures */ |
||
479 | bondari | 72 | |
480 | bondari | 73 | buddy_system_t * buddy_system; /**< buddy system for the zone */ |
354 | jermar | 74 | int flags; |
75 | }; |
||
76 | |||
77 | struct frame { |
||
537 | jermar | 78 | count_t refcount; /**< tracking of shared frames */ |
480 | bondari | 79 | __u8 buddy_order; /**< buddy system block order */ |
537 | jermar | 80 | link_t buddy_link; /**< link to the next free block inside one order */ |
759 | palkovsky | 81 | slab_slab_t *slab; /**< If allocated by slab, this points there */ |
374 | jermar | 82 | }; |
354 | jermar | 83 | |
533 | bondari | 84 | struct region { |
85 | __address base; |
||
86 | size_t size; |
||
87 | }; |
||
88 | |||
89 | extern region_t zone_blacklist[]; |
||
90 | extern count_t zone_blacklist_count; |
||
91 | extern void frame_region_not_free(__address base, size_t size); |
||
92 | extern void zone_create_in_region(__address base, size_t size); |
||
93 | |||
366 | jermar | 94 | extern spinlock_t zone_head_lock; /**< this lock protects zone_head list */ |
95 | extern link_t zone_head; /**< list of all zones in the system */ |
||
354 | jermar | 96 | |
368 | jermar | 97 | extern zone_t *zone_create(__address start, size_t size, int flags); |
98 | extern void zone_attach(zone_t *zone); |
||
1 | jermar | 99 | |
100 | extern void frame_init(void); |
||
368 | jermar | 101 | extern void frame_initialize(frame_t *frame, zone_t *zone); |
689 | bondari | 102 | |
103 | __address frame_alloc(int flags, __u8 order, int * status); |
||
1 | jermar | 104 | extern void frame_free(__address addr); |
689 | bondari | 105 | |
480 | bondari | 106 | zone_t * get_zone_by_frame(frame_t * frame); |
1 | jermar | 107 | |
366 | jermar | 108 | /* |
479 | bondari | 109 | * Buddy system operations |
110 | */ |
||
489 | jermar | 111 | link_t * zone_buddy_find_buddy(buddy_system_t *b, link_t * buddy); |
112 | link_t * zone_buddy_bisect(buddy_system_t *b, link_t * block); |
||
113 | link_t * zone_buddy_coalesce(buddy_system_t *b, link_t * buddy_l, link_t * buddy_r); |
||
114 | void zone_buddy_set_order(buddy_system_t *b, link_t * block, __u8 order); |
||
115 | __u8 zone_buddy_get_order(buddy_system_t *b, link_t * block); |
||
533 | bondari | 116 | void zone_buddy_mark_busy(buddy_system_t *b, link_t * block); |
479 | bondari | 117 | |
118 | /* |
||
366 | jermar | 119 | * TODO: Implement the following functions. |
120 | */ |
||
121 | extern frame_t *frame_reference(frame_t *frame); |
||
122 | extern void frame_release(frame_t *frame); |
||
123 | |||
676 | bondari | 124 | |
125 | /* |
||
126 | * Console functions |
||
127 | */ |
||
128 | extern void zone_print_list(void); |
||
683 | bondari | 129 | extern void zone_print_one(__address base); |
676 | bondari | 130 | |
1 | jermar | 131 | #endif |