Rev 489 | Rev 532 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 489 | Rev 501 | ||
---|---|---|---|
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 FRAME_KA 1 /* skip frames conflicting with user address space */ |
39 | #define FRAME_KA 1 /* skip frames conflicting with user address space */ |
40 | #define FRAME_PANIC 2 /* panic on failure */ |
40 | #define FRAME_PANIC 2 /* panic on failure */ |
41 | 41 | ||
42 | #define FRAME2ADDR(zone, frame) ((zone)->base + ((frame) - (zone)->frames) * FRAME_SIZE) |
42 | #define FRAME2ADDR(zone, frame) ((zone)->base + ((frame) - (zone)->frames) * FRAME_SIZE) |
43 | #define ADDR2FRAME(zone, addr) (&((zone)->frames[((addr) - (zone)->base) / FRAME_SIZE])) |
43 | #define ADDR2FRAME(zone, addr) (&((zone)->frames[((addr) - (zone)->base) / FRAME_SIZE])) |
44 | #define FRAME_INDEX(zone, frame) ((count_t)((frame) - (zone)->frames)) |
44 | #define FRAME_INDEX(zone, frame) ((count_t)((frame) - (zone)->frames)) |
45 | #define IS_BUDDY_LEFT_BLOCK(zone, frame) ((FRAME_INDEX((zone), (frame)) % (1 >> ((frame)->buddy_order + 1))) == 0) |
45 | #define IS_BUDDY_LEFT_BLOCK(zone, frame) ((FRAME_INDEX((zone), (frame)) % (1 >> ((frame)->buddy_order + 1))) == 0) |
46 | #define IS_BUDDY_RIGHT_BLOCK(zone, frame) ((FRAME_INDEX((zone), (frame)) % (1 >> ((frame)->buddy_order + 1))) == (1 >> (frame)->buddy_order)) |
46 | #define IS_BUDDY_RIGHT_BLOCK(zone, frame) ((FRAME_INDEX((zone), (frame)) % (1 >> ((frame)->buddy_order + 1))) == (1 >> (frame)->buddy_order)) |
47 | 47 | ||
48 | 48 | ||
49 | 49 | ||
50 | struct zone { |
50 | struct zone { |
51 | link_t link; /**< link to previous and next zone */ |
51 | link_t link; /**< link to previous and next zone */ |
52 | 52 | ||
53 | spinlock_t lock; /**< this lock protects everything below */ |
53 | spinlock_t lock; /**< this lock protects everything below */ |
54 | __address base; /**< physical address of the first frame in the frames array */ |
54 | __address base; /**< physical address of the first frame in the frames array */ |
55 | frame_t *frames; /**< array of frame_t structures in this zone */ |
55 | frame_t *frames; /**< array of frame_t structures in this zone */ |
56 | link_t free_head; /**< list of free frame_t structures */ |
56 | link_t free_head; /**< list of free frame_t structures */ |
57 | count_t free_count; /**< number of frame_t structures in free list */ |
57 | count_t free_count; /**< number of frame_t structures in free list */ |
58 | count_t busy_count; /**< number of frame_t structures not in free list */ |
58 | count_t busy_count; /**< number of frame_t structures not in free list */ |
59 | 59 | ||
60 | buddy_system_t * buddy_system; /**< buddy system for the zone */ |
60 | buddy_system_t * buddy_system; /**< buddy system for the zone */ |
61 | int flags; |
61 | int flags; |
62 | }; |
62 | }; |
63 | 63 | ||
64 | struct frame { |
64 | struct frame { |
65 | count_t refcount; /**< when == 0, the frame is in free list */ |
65 | count_t refcount; /**< when == 0, the frame is in free list */ |
66 | link_t link; /**< link to zone free list when refcount == 0 */ |
66 | link_t link; /**< link to zone free list when refcount == 0 */ |
67 | __u8 buddy_order; /**< buddy system block order */ |
67 | __u8 buddy_order; /**< buddy system block order */ |
68 | link_t buddy_link; /**< link to the next free block inside one order*/ |
68 | link_t buddy_link; /**< link to the next free block inside one order*/ |
69 | }; |
69 | }; |
70 | 70 | ||
71 | extern spinlock_t zone_head_lock; /**< this lock protects zone_head list */ |
71 | extern spinlock_t zone_head_lock; /**< this lock protects zone_head list */ |
72 | extern link_t zone_head; /**< list of all zones in the system */ |
72 | extern link_t zone_head; /**< list of all zones in the system */ |
73 | 73 | ||
74 | extern void zone_init(void); |
74 | extern void zone_init(void); |
75 | extern zone_t *zone_create(__address start, size_t size, int flags); |
75 | extern zone_t *zone_create(__address start, size_t size, int flags); |
76 | extern void zone_attach(zone_t *zone); |
76 | extern void zone_attach(zone_t *zone); |
77 | 77 | ||
78 | extern void frame_init(void); |
78 | extern void frame_init(void); |
79 | extern void frame_initialize(frame_t *frame, zone_t *zone); |
79 | extern void frame_initialize(frame_t *frame, zone_t *zone); |
80 | __address frame_alloc(int flags); |
80 | __address frame_alloc(int flags); |
81 | extern void frame_free(__address addr); |
81 | extern void frame_free(__address addr); |
82 | extern void frame_not_free(__address addr); |
82 | extern void frame_not_free(__address addr); |
83 | extern void frame_region_not_free(__address start, __address stop); |
83 | extern void frame_region_not_free(__address start, __address stop); |
84 | zone_t * get_zone_by_frame(frame_t * frame); |
84 | zone_t * get_zone_by_frame(frame_t * frame); |
85 | 85 | ||
86 | /* |
86 | /* |
87 | * Buddy system operations |
87 | * Buddy system operations |
88 | */ |
88 | */ |
89 | link_t * zone_buddy_find_buddy(buddy_system_t *b, link_t * buddy); |
89 | link_t * zone_buddy_find_buddy(buddy_system_t *b, link_t * buddy); |
90 | link_t * zone_buddy_bisect(buddy_system_t *b, link_t * block); |
90 | link_t * zone_buddy_bisect(buddy_system_t *b, link_t * block); |
91 | link_t * zone_buddy_coalesce(buddy_system_t *b, link_t * buddy_l, link_t * buddy_r); |
91 | link_t * zone_buddy_coalesce(buddy_system_t *b, link_t * buddy_l, link_t * buddy_r); |
92 | void zone_buddy_set_order(buddy_system_t *b, link_t * block, __u8 order); |
92 | void zone_buddy_set_order(buddy_system_t *b, link_t * block, __u8 order); |
93 | __u8 zone_buddy_get_order(buddy_system_t *b, link_t * block); |
93 | __u8 zone_buddy_get_order(buddy_system_t *b, link_t * block); |
94 | 94 | ||
95 | __address zone_buddy_frame_alloc(int flags, __u8 order); |
95 | __address zone_buddy_frame_alloc(int flags, __u8 order); |
96 | void zone_buddy_frame_free(__address addr); |
96 | void zone_buddy_frame_free(__address addr); |
97 | 97 | ||
98 | /* |
98 | /* |
99 | * TODO: Implement the following functions. |
99 | * TODO: Implement the following functions. |
100 | */ |
100 | */ |
101 | extern frame_t *frame_reference(frame_t *frame); |
101 | extern frame_t *frame_reference(frame_t *frame); |
102 | extern void frame_release(frame_t *frame); |
102 | extern void frame_release(frame_t *frame); |
103 | 103 | ||
104 | #endif |
104 | #endif |
105 | 105 |