Rev 548 | Rev 623 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 548 | Rev 564 | ||
---|---|---|---|
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 | 43 | ||
44 | #define FRAME2ADDR(zone, frame) ((zone)->base + ((frame) - (zone)->frames) * FRAME_SIZE) |
44 | #define FRAME2ADDR(zone, frame) ((zone)->base + ((frame) - (zone)->frames) * FRAME_SIZE) |
45 | #define ADDR2FRAME(zone, addr) (&((zone)->frames[((addr) - (zone)->base) / FRAME_SIZE])) |
45 | #define ADDR2FRAME(zone, addr) (&((zone)->frames[((addr) - (zone)->base) / FRAME_SIZE])) |
46 | #define FRAME_INDEX(zone, frame) ((index_t)((frame) - (zone)->frames)) |
46 | #define FRAME_INDEX(zone, frame) ((index_t)((frame) - (zone)->frames)) |
47 | #define FRAME_INDEX_VALID(zone, index) (((index) >= 0) && ((index) < ((zone)->free_count + (zone)->busy_count))) |
47 | #define FRAME_INDEX_VALID(zone, index) (((index) >= 0) && ((index) < ((zone)->free_count + (zone)->busy_count))) |
- | 48 | #define IS_BUDDY_ORDER_OK(index, order) ((~(((__native) -1) << (order)) & (index)) == 0) |
|
48 | #define IS_BUDDY_LEFT_BLOCK(zone, frame) ((FRAME_INDEX((zone), (frame)) & ~(((__native) -1)<<((frame)->buddy_order + 1))) == 0) |
49 | #define IS_BUDDY_LEFT_BLOCK(zone, frame) (((FRAME_INDEX((zone), (frame)) >> (frame)->buddy_order) & 0x1) == 0) |
- | 50 | #define IS_BUDDY_RIGHT_BLOCK(zone, frame) (((FRAME_INDEX((zone), (frame)) >> (frame)->buddy_order) & 0x1) == 1) |
|
49 | 51 | ||
50 | #define ZONE_BLACKLIST_SIZE 3 |
52 | #define ZONE_BLACKLIST_SIZE 3 |
51 | 53 | ||
52 | struct zone { |
54 | struct zone { |
53 | link_t link; /**< link to previous and next zone */ |
55 | link_t link; /**< link to previous and next zone */ |
54 | 56 | ||
55 | spinlock_t lock; /**< this lock protects everything below */ |
57 | spinlock_t lock; /**< this lock protects everything below */ |
56 | __address base; /**< physical address of the first frame in the frames array */ |
58 | __address base; /**< physical address of the first frame in the frames array */ |
57 | frame_t *frames; /**< array of frame_t structures in this zone */ |
59 | frame_t *frames; /**< array of frame_t structures in this zone */ |
58 | count_t free_count; /**< number of free frame_t structures */ |
60 | count_t free_count; /**< number of free frame_t structures */ |
59 | count_t busy_count; /**< number of busy frame_t structures */ |
61 | count_t busy_count; /**< number of busy frame_t structures */ |
60 | 62 | ||
61 | buddy_system_t * buddy_system; /**< buddy system for the zone */ |
63 | buddy_system_t * buddy_system; /**< buddy system for the zone */ |
62 | int flags; |
64 | int flags; |
63 | }; |
65 | }; |
64 | 66 | ||
65 | struct frame { |
67 | struct frame { |
66 | count_t refcount; /**< tracking of shared frames */ |
68 | count_t refcount; /**< tracking of shared frames */ |
67 | __u8 buddy_order; /**< buddy system block order */ |
69 | __u8 buddy_order; /**< buddy system block order */ |
68 | link_t buddy_link; /**< link to the next free block inside one order */ |
70 | link_t buddy_link; /**< link to the next free block inside one order */ |
69 | }; |
71 | }; |
70 | 72 | ||
71 | struct region { |
73 | struct region { |
72 | __address base; |
74 | __address base; |
73 | size_t size; |
75 | size_t size; |
74 | }; |
76 | }; |
75 | 77 | ||
76 | extern region_t zone_blacklist[]; |
78 | extern region_t zone_blacklist[]; |
77 | extern count_t zone_blacklist_count; |
79 | extern count_t zone_blacklist_count; |
78 | extern void frame_region_not_free(__address base, size_t size); |
80 | extern void frame_region_not_free(__address base, size_t size); |
79 | extern void zone_create_in_region(__address base, size_t size); |
81 | extern void zone_create_in_region(__address base, size_t size); |
80 | 82 | ||
81 | extern spinlock_t zone_head_lock; /**< this lock protects zone_head list */ |
83 | extern spinlock_t zone_head_lock; /**< this lock protects zone_head list */ |
82 | extern link_t zone_head; /**< list of all zones in the system */ |
84 | extern link_t zone_head; /**< list of all zones in the system */ |
83 | 85 | ||
84 | extern void zone_init(void); |
86 | extern void zone_init(void); |
85 | extern zone_t *zone_create(__address start, size_t size, int flags); |
87 | extern zone_t *zone_create(__address start, size_t size, int flags); |
86 | extern void zone_attach(zone_t *zone); |
88 | extern void zone_attach(zone_t *zone); |
87 | 89 | ||
88 | extern void frame_init(void); |
90 | extern void frame_init(void); |
89 | extern void frame_initialize(frame_t *frame, zone_t *zone); |
91 | extern void frame_initialize(frame_t *frame, zone_t *zone); |
90 | __address frame_alloc(int flags, __u8 order); |
92 | __address frame_alloc(int flags, __u8 order); |
91 | extern void frame_free(__address addr); |
93 | extern void frame_free(__address addr); |
92 | zone_t * get_zone_by_frame(frame_t * frame); |
94 | zone_t * get_zone_by_frame(frame_t * frame); |
93 | 95 | ||
94 | /* |
96 | /* |
95 | * Buddy system operations |
97 | * Buddy system operations |
96 | */ |
98 | */ |
97 | link_t * zone_buddy_find_buddy(buddy_system_t *b, link_t * buddy); |
99 | link_t * zone_buddy_find_buddy(buddy_system_t *b, link_t * buddy); |
98 | link_t * zone_buddy_bisect(buddy_system_t *b, link_t * block); |
100 | link_t * zone_buddy_bisect(buddy_system_t *b, link_t * block); |
99 | link_t * zone_buddy_coalesce(buddy_system_t *b, link_t * buddy_l, link_t * buddy_r); |
101 | link_t * zone_buddy_coalesce(buddy_system_t *b, link_t * buddy_l, link_t * buddy_r); |
100 | void zone_buddy_set_order(buddy_system_t *b, link_t * block, __u8 order); |
102 | void zone_buddy_set_order(buddy_system_t *b, link_t * block, __u8 order); |
101 | __u8 zone_buddy_get_order(buddy_system_t *b, link_t * block); |
103 | __u8 zone_buddy_get_order(buddy_system_t *b, link_t * block); |
102 | void zone_buddy_mark_busy(buddy_system_t *b, link_t * block); |
104 | void zone_buddy_mark_busy(buddy_system_t *b, link_t * block); |
103 | 105 | ||
104 | /* |
106 | /* |
105 | * TODO: Implement the following functions. |
107 | * TODO: Implement the following functions. |
106 | */ |
108 | */ |
107 | extern frame_t *frame_reference(frame_t *frame); |
109 | extern frame_t *frame_reference(frame_t *frame); |
108 | extern void frame_release(frame_t *frame); |
110 | extern void frame_release(frame_t *frame); |
109 | 111 | ||
110 | #endif |
112 | #endif |
111 | 113 |