Rev 689 | Rev 759 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 689 | Rev 724 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (C) 2005 Jakub Jermar |
2 | * Copyright (C) 2005 Jakub Jermar |
3 | * Copyright (C) 2005 Sergey Bondari |
3 | * Copyright (C) 2005 Sergey Bondari |
4 | * All rights reserved. |
4 | * All rights reserved. |
5 | * |
5 | * |
6 | * Redistribution and use in source and binary forms, with or without |
6 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted provided that the following conditions |
7 | * modification, are permitted provided that the following conditions |
8 | * are met: |
8 | * are met: |
9 | * |
9 | * |
10 | * - Redistributions of source code must retain the above copyright |
10 | * - Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. |
11 | * notice, this list of conditions and the following disclaimer. |
12 | * - Redistributions in binary form must reproduce the above copyright |
12 | * - Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the |
13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
16 | * derived from this software without specific prior written permission. |
17 | * |
17 | * |
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | */ |
28 | */ |
29 | 29 | ||
30 | #ifndef __FRAME_H__ |
30 | #ifndef __FRAME_H__ |
31 | #define __FRAME_H__ |
31 | #define __FRAME_H__ |
32 | 32 | ||
33 | #include <arch/types.h> |
33 | #include <arch/types.h> |
34 | #include <typedefs.h> |
34 | #include <typedefs.h> |
35 | #include <list.h> |
35 | #include <list.h> |
36 | #include <synch/spinlock.h> |
36 | #include <synch/spinlock.h> |
37 | #include <mm/buddy.h> |
37 | #include <mm/buddy.h> |
38 | 38 | ||
39 | #define ONE_FRAME 0 |
39 | #define ONE_FRAME 0 |
40 | 40 | ||
41 | #define FRAME_KA 1 /* skip frames conflicting with user address space */ |
41 | #define FRAME_KA 1 /* skip frames conflicting with user address space */ |
42 | #define FRAME_PANIC 2 /* panic on failure */ |
42 | #define FRAME_PANIC 2 /* panic on failure */ |
43 | #define FRAME_NON_BLOCKING 4 /* do not panic and do not sleep on failure */ |
43 | #define FRAME_NON_BLOCKING 4 /* do not panic and do not sleep on failure */ |
44 | 44 | ||
45 | #define FRAME_OK 0 /* frame_alloc return status */ |
45 | #define FRAME_OK 0 /* frame_alloc return status */ |
46 | #define FRAME_NO_MEMORY 1 /* frame_alloc return status */ |
46 | #define FRAME_NO_MEMORY 1 /* frame_alloc return status */ |
47 | #define FRAME_ERROR 2 /* frame_alloc return status */ |
47 | #define FRAME_ERROR 2 /* frame_alloc return status */ |
48 | 48 | ||
49 | #define FRAME2ADDR(zone, frame) ((zone)->base + ((frame) - (zone)->frames) * FRAME_SIZE) |
49 | #define FRAME2ADDR(zone, frame) ((zone)->base + ((frame) - (zone)->frames) * FRAME_SIZE) |
50 | #define ADDR2FRAME(zone, addr) (&((zone)->frames[((addr) - (zone)->base) / FRAME_SIZE])) |
50 | #define ADDR2FRAME(zone, addr) (&((zone)->frames[((addr) - (zone)->base) / FRAME_SIZE])) |
51 | #define FRAME_INDEX(zone, frame) ((index_t)((frame) - (zone)->frames)) |
51 | #define FRAME_INDEX(zone, frame) ((index_t)((frame) - (zone)->frames)) |
- | 52 | #define FRAME_INDEX_ABS(zone, frame) (((index_t)((frame) - (zone)->frames)) + (zone)->base_index) |
|
52 | #define FRAME_INDEX_VALID(zone, index) (((index) >= 0) && ((index) < ((zone)->free_count + (zone)->busy_count))) |
53 | #define FRAME_INDEX_VALID(zone, index) (((index) >= 0) && ((index) < ((zone)->free_count + (zone)->busy_count))) |
53 | #define IS_BUDDY_ORDER_OK(index, order) ((~(((__native) -1) << (order)) & (index)) == 0) |
54 | #define IS_BUDDY_ORDER_OK(index, order) ((~(((__native) -1) << (order)) & (index)) == 0) |
54 | #define IS_BUDDY_LEFT_BLOCK(zone, frame) (((FRAME_INDEX((zone), (frame)) >> (frame)->buddy_order) & 0x1) == 0) |
55 | #define IS_BUDDY_LEFT_BLOCK(zone, frame) (((FRAME_INDEX((zone), (frame)) >> (frame)->buddy_order) & 0x1) == 0) |
55 | #define IS_BUDDY_RIGHT_BLOCK(zone, frame) (((FRAME_INDEX((zone), (frame)) >> (frame)->buddy_order) & 0x1) == 1) |
56 | #define IS_BUDDY_RIGHT_BLOCK(zone, frame) (((FRAME_INDEX((zone), (frame)) >> (frame)->buddy_order) & 0x1) == 1) |
- | 57 | #define IS_BUDDY_LEFT_BLOCK_ABS(zone, frame) (((FRAME_INDEX_ABS((zone), (frame)) >> (frame)->buddy_order) & 0x1) == 0) |
|
- | 58 | #define IS_BUDDY_RIGHT_BLOCK_ABS(zone, frame) (((FRAME_INDEX_ABS((zone), (frame)) >> (frame)->buddy_order) & 0x1) == 1) |
|
56 | 59 | ||
57 | #define ZONE_BLACKLIST_SIZE 4 |
60 | #define ZONE_BLACKLIST_SIZE 4 |
58 | 61 | ||
59 | struct zone { |
62 | struct zone { |
60 | link_t link; /**< link to previous and next zone */ |
63 | link_t link; /**< link to previous and next zone */ |
61 | 64 | ||
62 | SPINLOCK_DECLARE(lock); /**< this lock protects everything below */ |
65 | SPINLOCK_DECLARE(lock); /**< this lock protects everything below */ |
63 | __address base; /**< physical address of the first frame in the frames array */ |
66 | __address base; /**< physical address of the first frame in the frames array */ |
- | 67 | index_t base_index; /**< frame index offset of the zone base */ |
|
64 | frame_t *frames; /**< array of frame_t structures in this zone */ |
68 | frame_t *frames; /**< array of frame_t structures in this zone */ |
65 | count_t free_count; /**< number of free frame_t structures */ |
69 | count_t free_count; /**< number of free frame_t structures */ |
66 | count_t busy_count; /**< number of busy frame_t structures */ |
70 | count_t busy_count; /**< number of busy frame_t structures */ |
67 | 71 | ||
68 | buddy_system_t * buddy_system; /**< buddy system for the zone */ |
72 | buddy_system_t * buddy_system; /**< buddy system for the zone */ |
69 | int flags; |
73 | int flags; |
70 | }; |
74 | }; |
71 | 75 | ||
72 | struct frame { |
76 | struct frame { |
73 | count_t refcount; /**< tracking of shared frames */ |
77 | count_t refcount; /**< tracking of shared frames */ |
74 | __u8 buddy_order; /**< buddy system block order */ |
78 | __u8 buddy_order; /**< buddy system block order */ |
75 | link_t buddy_link; /**< link to the next free block inside one order */ |
79 | link_t buddy_link; /**< link to the next free block inside one order */ |
76 | }; |
80 | }; |
77 | 81 | ||
78 | struct region { |
82 | struct region { |
79 | __address base; |
83 | __address base; |
80 | size_t size; |
84 | size_t size; |
81 | }; |
85 | }; |
82 | 86 | ||
83 | extern region_t zone_blacklist[]; |
87 | extern region_t zone_blacklist[]; |
84 | extern count_t zone_blacklist_count; |
88 | extern count_t zone_blacklist_count; |
85 | extern void frame_region_not_free(__address base, size_t size); |
89 | extern void frame_region_not_free(__address base, size_t size); |
86 | extern void zone_create_in_region(__address base, size_t size); |
90 | extern void zone_create_in_region(__address base, size_t size); |
87 | 91 | ||
88 | extern spinlock_t zone_head_lock; /**< this lock protects zone_head list */ |
92 | extern spinlock_t zone_head_lock; /**< this lock protects zone_head list */ |
89 | extern link_t zone_head; /**< list of all zones in the system */ |
93 | extern link_t zone_head; /**< list of all zones in the system */ |
90 | 94 | ||
91 | extern zone_t *zone_create(__address start, size_t size, int flags); |
95 | extern zone_t *zone_create(__address start, size_t size, int flags); |
92 | extern void zone_attach(zone_t *zone); |
96 | extern void zone_attach(zone_t *zone); |
93 | 97 | ||
94 | extern void frame_init(void); |
98 | extern void frame_init(void); |
95 | extern void frame_initialize(frame_t *frame, zone_t *zone); |
99 | extern void frame_initialize(frame_t *frame, zone_t *zone); |
96 | 100 | ||
97 | __address frame_alloc(int flags, __u8 order, int * status); |
101 | __address frame_alloc(int flags, __u8 order, int * status); |
98 | extern void frame_free(__address addr); |
102 | extern void frame_free(__address addr); |
99 | 103 | ||
100 | zone_t * get_zone_by_frame(frame_t * frame); |
104 | zone_t * get_zone_by_frame(frame_t * frame); |
101 | 105 | ||
102 | /* |
106 | /* |
103 | * Buddy system operations |
107 | * Buddy system operations |
104 | */ |
108 | */ |
105 | link_t * zone_buddy_find_buddy(buddy_system_t *b, link_t * buddy); |
109 | link_t * zone_buddy_find_buddy(buddy_system_t *b, link_t * buddy); |
106 | link_t * zone_buddy_bisect(buddy_system_t *b, link_t * block); |
110 | link_t * zone_buddy_bisect(buddy_system_t *b, link_t * block); |
107 | link_t * zone_buddy_coalesce(buddy_system_t *b, link_t * buddy_l, link_t * buddy_r); |
111 | link_t * zone_buddy_coalesce(buddy_system_t *b, link_t * buddy_l, link_t * buddy_r); |
108 | void zone_buddy_set_order(buddy_system_t *b, link_t * block, __u8 order); |
112 | void zone_buddy_set_order(buddy_system_t *b, link_t * block, __u8 order); |
109 | __u8 zone_buddy_get_order(buddy_system_t *b, link_t * block); |
113 | __u8 zone_buddy_get_order(buddy_system_t *b, link_t * block); |
110 | void zone_buddy_mark_busy(buddy_system_t *b, link_t * block); |
114 | void zone_buddy_mark_busy(buddy_system_t *b, link_t * block); |
111 | 115 | ||
112 | /* |
116 | /* |
113 | * TODO: Implement the following functions. |
117 | * TODO: Implement the following functions. |
114 | */ |
118 | */ |
115 | extern frame_t *frame_reference(frame_t *frame); |
119 | extern frame_t *frame_reference(frame_t *frame); |
116 | extern void frame_release(frame_t *frame); |
120 | extern void frame_release(frame_t *frame); |
117 | 121 | ||
118 | 122 | ||
119 | /* |
123 | /* |
120 | * Console functions |
124 | * Console functions |
121 | */ |
125 | */ |
122 | extern void zone_print_list(void); |
126 | extern void zone_print_list(void); |
123 | extern void zone_print_one(__address base); |
127 | extern void zone_print_one(__address base); |
124 | 128 | ||
125 | #endif |
129 | #endif |
126 | 130 |