]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/l4sys/include/kip
update
[l4.git] / l4 / pkg / l4sys / include / kip
1 // vim:set ft=cpp:
2 /*!
3  * \file
4  * \brief  L4::Kip class, memory descriptors.
5  *
6  * \author Alexander Warg <alexander.warg@os.inf.tu-dresden.de>
7  * \ingroup l4_api
8  *
9  */
10 /*
11  * (c) 2008-2009 Author(s)
12  *     economic rights: Technische Universität Dresden (Germany)
13  *
14  * This file is part of TUD:OS and distributed under the terms of the
15  * GNU General Public License 2.
16  * Please see the COPYING-GPL-2 file for details.
17  *
18  * As a special exception, you may use this file as part of a free software
19  * library without restriction.  Specifically, if other files instantiate
20  * templates or use macros or inline functions from this file, or you compile
21  * this file and link it with other files to produce an executable, this
22  * file does not by itself cause the resulting executable to be covered by
23  * the GNU General Public License.  This exception does not however
24  * invalidate any other reasons why the executable file might be covered by
25  * the GNU General Public License.
26  */
27 #ifndef L4_SYS_KIP_H__
28 #define L4_SYS_KIP_H__
29
30 /* C++ version of memory descriptors */
31
32 /**
33  * \defgroup l4_kip_api Kernel Interface Page
34  * \ingroup l4_api
35  * \brief Kernel Interface Page.
36  *
37  * C++ interface for the Kernel Interface Page:<br>
38  * <c>\#include <l4/sys/kip></c>
39  */
40
41 namespace L4
42 {
43   namespace Kip
44   {
45     /**
46      * \brief Memory descriptors stored in the kernel interface page.
47      * \ingroup l4_kip_api
48      *
49      * <c>\#include <l4/sys/kip></c>
50      */
51     class Mem_desc
52     {
53     public:
54       /**
55        * \brief Memory types.
56        */
57       enum Mem_type
58       {
59         Undefined    = 0x0,  //< Undefined memory
60         Conventional = 0x1,  //< Conventional memory
61         Reserved     = 0x2,  //< Reserved region, do not use this memory
62         Dedicated    = 0x3,  //< Dedicated
63         Shared       = 0x4,  //< Shared
64
65         Bootloader   = 0xe,  //< Memory belongs to the boot loader
66         Arch         = 0xf   //< Architecture specific memory
67       };
68     private:
69       unsigned long _l, _h;
70
71       static unsigned long &memory_info(void *kip) throw()
72       { return *((unsigned long *)kip + 21); }
73
74       static unsigned long memory_info(void const *kip) throw()
75       { return *((unsigned long const *)kip + 21); }
76
77     public:
78       /**
79        * \brief Get first memory descriptor.
80        * \param kip  Pointer to the kernel info page
81        * \return First memory descriptor stored in the kernel info page
82        */
83       static Mem_desc *first(void *kip) throw()
84       {
85         return (Mem_desc*)((char *)kip
86             + (memory_info(kip) >> ((sizeof(unsigned long)/2)*8)));
87       }
88
89       static Mem_desc const *first(void const *kip) throw()
90       {
91         return (Mem_desc const*)((char const *)kip
92             + (memory_info(kip) >> ((sizeof(unsigned long)/2)*8)));
93       }
94
95       /**
96        * \brief Return number of memory descriptors stored in the kernel info page.
97        * \param kip  Pointer to the kernel info page
98        * \return Number of memory descriptors in the kernel info page.
99        */
100       static unsigned long count(void const *kip) throw()
101       {
102         return memory_info(kip) & ((1UL << ((sizeof(unsigned long)/2)*8)) - 1);
103       }
104
105       /**
106        * \brief Set number of memory descriptors.
107        * \param kip   Pointer to the kernel info page
108        * \param count Number of memory descriptors
109        */
110       static void count(void *kip, unsigned count) throw()
111       {
112         unsigned long &mi = memory_info(kip);
113         mi = (mi & ~((1UL << ((sizeof(unsigned long)/2)*8)) - 1)) | count;
114       }
115
116       /**
117        * \brief Initialize memory descriptor.
118        * \param start  Start address
119        * \param end    End address
120        * \param t      Memory type
121        * \param st     Memory subtype, defaults to 0
122        * \param virt   True for virtual memory, false for physical memory,
123        *               defaults to physical
124        */
125       Mem_desc(unsigned long start, unsigned long end,
126                Mem_type t, unsigned char st = 0, bool virt = false) throw()
127       : _l((start & ~0x3ffUL) | (t & 0x0f) | ((st << 4) & 0x0f0)
128            | (virt?0x0200:0x0)), _h(end | 0x3ffUL)
129       {}
130
131       /**
132        * \brief Return start address of memory descriptor.
133        * \return Start address of memory descriptor
134        */
135       unsigned long start() const throw() { return _l & ~0x3ffUL; }
136
137       /**
138        * \brief Return end address of memory descriptor.
139        * \return End address of memory descriptor
140        */
141       unsigned long end() const throw() { return _h | 0x3ffUL; }
142
143       /**
144        * \brief Return size of region described by the memory descriptor.
145        * \return Size of the region described by the memory descriptor
146        */
147       unsigned long size() const throw() { return end() + 1 - start(); }
148
149       /**
150        * \brief Return type of the memory descriptor.
151        * \return Type of the memory descriptor
152        */
153       Mem_type type() const throw() { return (Mem_type)(_l & 0x0f); }
154
155       /**
156        * \brief Return sub-type of the memory descriptor.
157        * \return Sub-type of the memory descriptor
158        */
159       unsigned char sub_type() const throw() { return (_l >> 4) & 0x0f; }
160
161       /**
162        * \brief Return whether the memory descriptor describes a virtual or
163        * physical region.
164        * \return True for virtual region, false for physical region.
165        */
166       unsigned is_virtual() const throw() { return _l & 0x200; }
167
168       /**
169        * \brief Set values of a memory descriptor.
170        * \param start  Start address
171        * \param end    End address
172        * \param t      Memory type
173        * \param st     Sub-type, defaults to 0
174        * \param virt   Virtual or physical memory region, defaults to physical
175        */
176       void set(unsigned long start, unsigned long end,
177                Mem_type t, unsigned char st = 0, bool virt = false) throw()
178       {
179         _l = (start & ~0x3ffUL) | (t & 0x0f) | ((st << 4) & 0x0f0)
180              | (virt?0x0200:0x0);
181
182         _h = end | 0x3ffUL;
183       }
184
185     };
186   };
187 };
188
189 #endif