Rev 11 | Rev 17 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
9 | bondari | 1 | <?xml version="1.0" encoding="UTF-8"?> |
11 | bondari | 2 | <chapter id="mm"> |
3 | <?dbhtml filename="mm.html"?> |
||
9 | bondari | 4 | |
11 | bondari | 5 | <title>Memory management</title> |
9 | bondari | 6 | |
11 | bondari | 7 | <section> |
8 | <title>Virtual memory management</title> |
||
9 | bondari | 9 | |
10 | <section> |
||
11 | bondari | 11 | <title>Address spaces</title> |
9 | bondari | 12 | |
13 | <para></para> |
||
14 | </section> |
||
15 | |||
16 | <section> |
||
11 | bondari | 17 | <title>Virtual address translation</title> |
9 | bondari | 18 | |
19 | <para></para> |
||
20 | </section> |
||
11 | bondari | 21 | </section> |
9 | bondari | 22 | |
11 | bondari | 23 | <section> |
24 | <title>Physical memory management</title> |
||
9 | bondari | 25 | |
11 | bondari | 26 | <section id="zones_and_frames"> |
27 | <title>Zones and frames</title> |
||
9 | bondari | 28 | |
11 | bondari | 29 | <para>Physical memory is divided into zones. Each zone represents |
30 | continuous area of physical memory frames. Allocation of frames is |
||
31 | handled by the <link linkend="buddy_allocator">buddy allocator</link> |
||
32 | associated with the zone. Zone also contains information about free and |
||
33 | occupied frames and its base addresss in the memory. Some of the |
||
34 | architectures (Mips, PPC) have only one zone, that covers whole physical |
||
35 | memory. Other architectures (IA32) have multiple zones.</para> |
||
36 | </section> |
||
9 | bondari | 37 | |
11 | bondari | 38 | <section id="buddy_allocator"> |
39 | <title>Buddy allocator</title> |
||
9 | bondari | 40 | |
15 | bondari | 41 | <section> |
42 | <title>Overview</title> |
||
9 | bondari | 43 | |
15 | bondari | 44 | <para>Physical memory allocation inside one <link |
45 | linkend="zones_and_frames">memory zone</link> is being handled by |
||
46 | buddy allocation system. This approach greatly reduces possibility of |
||
47 | outer memory fragmentation and helps in allocating bigger continious |
||
48 | blocks of physical memory aligned to their size. Problem of inner |
||
49 | memory fragmentation is being solved by <link linkend="slab">SLAB |
||
50 | allocation system.</link></para> |
||
11 | bondari | 51 | |
15 | bondari | 52 | <graphic fileref="images/mm1.png" /> |
53 | |||
54 | <para>Frames are grouped into bigger blocks and blocks of the size |
||
55 | <mathphrase>2<superscript>i</superscript></mathphrase> are stored in |
||
56 | the list indexed with <varname>i</varname> (so called order index). If |
||
57 | list contains 2 ajacent blocks (of a same size of cause) they can be |
||
58 | merged into the bigger one and moved into the list with higher order |
||
59 | index, thus making possible allocation of a bigger block.</para> |
||
60 | </section> |
||
61 | |||
62 | <section> |
||
63 | <title>Implementation</title> |
||
64 | |||
65 | <formalpara> |
||
66 | <title>Data organization</title> |
||
67 | |||
68 | <para>Buddy allocator always uses first frame to represent frame |
||
69 | block. This frame contains <varname>buddy_order</varname> variable |
||
70 | to provide information about the block size it actually represents ( |
||
71 | <mathphrase>2<superscript>buddy_order</superscript></mathphrase> |
||
72 | frames block). Other frames in block have this value set to magic |
||
73 | <constant>BUDDY_INNER_BLOCK</constant> that is much greater than |
||
74 | buddy <varname>max_order</varname> value.</para> |
||
75 | |||
76 | <para>Each <varname>frame_t</varname> also contains pointer member |
||
77 | to hold frame structure in the linked list inside one order.</para> |
||
78 | </formalpara> |
||
79 | |||
80 | <formalpara> |
||
81 | <title>Allocation algorithm</title> |
||
82 | |||
83 | <para>Upon <mathphrase>2<superscript>i</superscript></mathphrase> |
||
84 | frames block allocation request, allocator checks if there are any |
||
85 | blocks available at the order list <varname>i</varname>. If yes, |
||
86 | removes block from order list and returns its address. If no, |
||
87 | recursively allocates |
||
88 | <mathphrase>2<superscript>i+1</superscript></mathphrase> frame |
||
89 | block, splits it into two |
||
90 | <mathphrase>2<superscript>i</superscript></mathphrase> frame blocks. |
||
91 | Then adds one of the blocks to the <varname>i</varname> order list |
||
92 | and returns address of another.</para> |
||
93 | </formalpara> |
||
94 | |||
95 | <formalpara> |
||
96 | <title>Deallocation algorithm</title> |
||
97 | |||
98 | <para>Check if block has so called buddy (another free |
||
99 | <mathphrase>2<superscript>i</superscript></mathphrase> frame block |
||
100 | that can be linked with freed block into the |
||
101 | <mathphrase>2<superscript>i+1</superscript></mathphrase> block). |
||
102 | Technically, buddy is a odd/even block for even/odd block |
||
103 | respectively. Plus we can put an extra requirement, that resulting |
||
104 | block must be aligned to its size. This requirement guarantees |
||
105 | natural block alignment for the blocks coming out the allocation |
||
106 | system. |
||
107 | </para> |
||
108 | |||
109 | <para> |
||
110 | Using direct pointer arithmetics, <varname>frame_t::ref_count</varname> and <varname>frame_t::buddy_order</varname> variables, |
||
111 | finding buddy is done at constant time. |
||
112 | </para> |
||
113 | </formalpara> |
||
114 | </section> |
||
9 | bondari | 115 | </section> |
116 | |||
15 | bondari | 117 | <section id="slab"> |
11 | bondari | 118 | <title>Slab allocator</title> |
9 | bondari | 119 | |
11 | bondari | 120 | <para>Kernel memory allocation is handled by slab.</para> |
9 | bondari | 121 | </section> |
122 | |||
15 | bondari | 123 | <section> |
124 | <title>Memory sharing</title> |
||
9 | bondari | 125 | |
15 | bondari | 126 | <para>Not implemented yet(?)</para> |
127 | </section> |
||
11 | bondari | 128 | </section> |
129 | </chapter> |