Subversion Repositories HelenOS-historic

Rev

Rev 778 | Rev 788 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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