Subversion Repositories HelenOS-historic

Rev

Rev 788 | Rev 816 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 788 Rev 814
Line 33... Line 33...
33
#include <arch/types.h>
33
#include <arch/types.h>
34
#include <typedefs.h>
34
#include <typedefs.h>
35
#include <adt/list.h>
35
#include <adt/list.h>
36
#include <synch/spinlock.h>
36
#include <synch/spinlock.h>
37
#include <mm/buddy.h>
37
#include <mm/buddy.h>
38
#include <mm/slab.h>
38
#include <arch/mm/page.h>
39
 
39
 
40
#define ONE_FRAME   0
40
#define ONE_FRAME   0
41
 
41
 
-
 
42
#define ZONES_MAX       16      /**< Maximum number of zones in system */
-
 
43
 
-
 
44
#define ZONE_JOIN       0x1  /**< If possible, merge with neighberhood zones */
-
 
45
 
-
 
46
 
42
#define FRAME_KA        0x1 /* skip frames conflicting with user address space */
47
#define FRAME_KA        0x1 /* skip frames conflicting with user address space */
43
#define FRAME_PANIC     0x2 /* panic on failure */
48
#define FRAME_PANIC     0x2 /* panic on failure */
44
#define FRAME_ATOMIC            0x4 /* do not panic and do not sleep on failure */
49
#define FRAME_ATOMIC            0x4 /* do not panic and do not sleep on failure */
45
#define FRAME_NO_RECLAIM        0x8     /* Do not start reclaiming when no free memory */
50
#define FRAME_NO_RECLAIM        0x8     /* Do not start reclaiming when no free memory */
46
 
51
 
47
#define FRAME_OK        0   /* frame_alloc return status */
52
#define FRAME_OK        0   /* frame_alloc return status */
48
#define FRAME_NO_MEMORY     1   /* frame_alloc return status */
53
#define FRAME_NO_MEMORY     1   /* frame_alloc return status */
49
#define FRAME_ERROR     2   /* frame_alloc return status */
54
#define FRAME_ERROR     2   /* frame_alloc return status */
50
 
55
 
-
 
56
/* Return true if the interlvals overlap */
51
#define FRAME2ADDR(zone, frame)         ((zone)->base + (((frame) - (zone)->frames) << FRAME_WIDTH))
57
static inline int overlaps(__address s1,__address e1, __address s2, __address e2)
-
 
58
{
-
 
59
    if (s1 >= s2 && s1 < e2)
-
 
60
        return 1;
-
 
61
    if (e1 >= s2 && e1 < e2)
-
 
62
        return 1;
-
 
63
    if ((s1 < s2) && (e1 >= e2))
-
 
64
        return 1;
-
 
65
    return 0;
-
 
66
}
-
 
67
 
-
 
68
static inline __address PFN2ADDR(pfn_t frame)
-
 
69
{
52
#define ADDR2FRAME(zone, addr)          (&((zone)->frames[(((addr) - (zone)->base) >> FRAME_WIDTH)]))
70
    return (__address)(frame << PAGE_WIDTH);
-
 
71
}
-
 
72
 
53
#define FRAME_INDEX(zone, frame)        ((index_t)((frame) - (zone)->frames))
73
static inline pfn_t ADDR2PFN(__address addr)
-
 
74
{
-
 
75
    return (pfn_t)(addr >> PAGE_WIDTH);
-
 
76
}
-
 
77
 
54
#define FRAME_INDEX_ABS(zone, frame)        (((index_t)((frame) - (zone)->frames)) + (zone)->base_index)
78
static inline pfn_t SIZE2PFN(__address size)
-
 
79
{
-
 
80
    if (!size)
-
 
81
        return 0;
55
#define FRAME_INDEX_VALID(zone, index)      (((index) >= 0) && ((index) < ((zone)->free_count + (zone)->busy_count)))
82
    return (pfn_t)((size-1) >> PAGE_WIDTH)+1;
-
 
83
}
-
 
84
 
56
#define IS_BUDDY_ORDER_OK(index, order)     ((~(((__native) -1) << (order)) & (index)) == 0)
85
#define IS_BUDDY_ORDER_OK(index, order)     ((~(((__native) -1) << (order)) & (index)) == 0)
57
#define IS_BUDDY_LEFT_BLOCK(zone, frame)    (((FRAME_INDEX((zone), (frame)) >> (frame)->buddy_order) & 0x1) == 0)
86
#define IS_BUDDY_LEFT_BLOCK(zone, frame)    (((frame_index((zone), (frame)) >> (frame)->buddy_order) & 0x1) == 0)
58
#define IS_BUDDY_RIGHT_BLOCK(zone, frame)   (((FRAME_INDEX((zone), (frame)) >> (frame)->buddy_order) & 0x1) == 1)
87
#define IS_BUDDY_RIGHT_BLOCK(zone, frame)   (((frame_index((zone), (frame)) >> (frame)->buddy_order) & 0x1) == 1)
59
#define IS_BUDDY_LEFT_BLOCK_ABS(zone, frame)    (((FRAME_INDEX_ABS((zone), (frame)) >> (frame)->buddy_order) & 0x1) == 0)
88
#define IS_BUDDY_LEFT_BLOCK_ABS(zone, frame)    (((frame_index_abs((zone), (frame)) >> (frame)->buddy_order) & 0x1) == 0)
60
#define IS_BUDDY_RIGHT_BLOCK_ABS(zone, frame)   (((FRAME_INDEX_ABS((zone), (frame)) >> (frame)->buddy_order) & 0x1) == 1)
89
#define IS_BUDDY_RIGHT_BLOCK_ABS(zone, frame)   (((frame_index_abs((zone), (frame)) >> (frame)->buddy_order) & 0x1) == 1)
61
 
90
 
62
#define ZONE_BLACKLIST_SIZE 8
-
 
63
 
91
 
64
#define frame_alloc(order, flags)               frame_alloc_generic(order, flags, NULL, NULL)
92
#define frame_alloc(order, flags)               frame_alloc_generic(order, flags, NULL, NULL)
65
#define frame_alloc_rc(order, flags, status)            frame_alloc_generic(order, flags, status, NULL)
93
#define frame_alloc_rc(order, flags, status)            frame_alloc_generic(order, flags, status, NULL)
66
#define frame_alloc_rc_zone(order, flags, status, zone)     frame_alloc_generic(order, flags, status, zone)
94
#define frame_alloc_rc_zone(order, flags, status, zone)     frame_alloc_generic(order, flags, status, zone)
67
 
95
 
68
struct zone {
-
 
69
    link_t link;        /**< link to previous and next zone */
-
 
70
 
-
 
71
    SPINLOCK_DECLARE(lock); /**< this lock protects everything below */
-
 
72
    __address base;     /**< physical address of the first frame in the frames array */
-
 
73
    index_t base_index; /**< frame index offset of the zone base */
-
 
74
    frame_t *frames;    /**< array of frame_t structures in this zone */
-
 
75
    count_t free_count; /**< number of free frame_t structures */
-
 
76
    count_t busy_count; /**< number of busy frame_t structures */
-
 
77
   
-
 
78
    buddy_system_t * buddy_system; /**< buddy system for the zone */
-
 
79
    int flags;
-
 
80
};
-
 
81
 
-
 
82
struct frame {
-
 
83
    count_t refcount;   /**< tracking of shared frames  */
-
 
84
    __u8 buddy_order;   /**< buddy system block order */
-
 
85
    link_t buddy_link;  /**< link to the next free block inside one order */
-
 
86
    void *parent;           /**< If allocated by slab, this points there */
-
 
87
};
-
 
88
 
-
 
89
struct region {
-
 
90
    __address base;
-
 
91
    size_t size;
-
 
92
};
-
 
93
 
-
 
94
extern region_t zone_blacklist[];
-
 
95
extern count_t zone_blacklist_count;
-
 
96
extern void frame_region_not_free(__address base, size_t size);
-
 
97
extern void zone_create_in_region(__address base, size_t size);
-
 
98
 
-
 
99
extern spinlock_t zone_head_lock;   /**< this lock protects zone_head list */
-
 
100
extern link_t zone_head;        /**< list of all zones in the system */
-
 
101
 
-
 
102
extern zone_t *zone_create(__address start, size_t size, int flags);
-
 
103
extern void zone_attach(zone_t *zone);
-
 
104
 
-
 
105
extern void frame_init(void);
96
extern void frame_init(void);
106
extern void frame_initialize(frame_t *frame, zone_t *zone);
-
 
107
 
-
 
108
__address frame_alloc_generic(__u8 order, int flags, int * status, zone_t **pzone);
97
__address frame_alloc_generic(__u8 order, int flags, int * status, int *pzone);
109
 
-
 
110
 
-
 
111
extern void frame_free(__address addr);
98
extern void frame_free(__address addr);
112
 
99
 
113
zone_t * get_zone_by_frame(frame_t * frame);
-
 
114
 
-
 
115
/*
-
 
116
 * Buddy system operations
-
 
117
 */
-
 
118
link_t * zone_buddy_find_buddy(buddy_system_t *b, link_t * buddy);
-
 
119
link_t * zone_buddy_bisect(buddy_system_t *b, link_t * block);
-
 
120
link_t * zone_buddy_coalesce(buddy_system_t *b, link_t * buddy_l, link_t * buddy_r);
-
 
121
void zone_buddy_set_order(buddy_system_t *b, link_t * block, __u8 order);
100
extern void zone_create(pfn_t start, pfn_t count, pfn_t confframe, int flags);
122
__u8 zone_buddy_get_order(buddy_system_t *b, link_t * block);
-
 
123
void zone_buddy_mark_busy(buddy_system_t *b, link_t * block);
-
 
124
extern frame_t * frame_addr2frame(__address addr);
-
 
125
 
-
 
126
/*
-
 
127
 * TODO: Implement the following functions.
-
 
128
 */
-
 
129
extern frame_t *frame_reference(frame_t *frame);
-
 
130
extern void frame_release(frame_t *frame);
-
 
131
 
101
 
-
 
102
void * frame_get_parent(pfn_t frame, int hint);
-
 
103
void frame_set_parent(pfn_t frame, void *data, int hint);
-
 
104
void frame_mark_unavailable(pfn_t start, pfn_t count);
-
 
105
__address zone_conf_size(pfn_t start, pfn_t count);
132
 
106
 
133
/*
107
/*
134
 * Console functions
108
 * Console functions
135
 */
109
 */
136
extern void zone_print_list(void);
110
extern void zone_print_list(void);
137
extern void zone_print_one(__address base);
111
void zone_print_one(int znum);
138
 
112
 
139
#endif
113
#endif