Subversion Repositories HelenOS

Rev

Rev 2238 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2128 jermar 1
/*
2238 kebrt 2
 * Copyright (c) 2007 Pavel Jancik, Michal Kebrt
2128 jermar 3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
29
/** @addtogroup arm32mm
30
 * @{
31
 */
32
/** @file
33
 */
34
 
35
#ifndef KERN_arm32_PAGE_H_
36
#define KERN_arm32_PAGE_H_
37
 
38
#include <arch/mm/frame.h>
2182 jancik 39
#include <mm/mm.h>
40
#include <arch/exception.h>
2128 jermar 41
 
2182 jancik 42
 
2128 jermar 43
#define PAGE_WIDTH  FRAME_WIDTH
44
#define PAGE_SIZE   FRAME_SIZE
45
 
46
#define PAGE_COLOR_BITS 0           /* dummy */
47
 
48
#ifndef __ASM__
49
#   define KA2PA(x) (((uintptr_t) (x)) - 0x80000000)
50
#   define PA2KA(x) (((uintptr_t) (x)) + 0x80000000)
51
#else
52
#   define KA2PA(x) ((x) - 0x80000000)
53
#   define PA2KA(x) ((x) + 0x80000000)
54
#endif
55
 
56
#ifdef KERNEL
57
 
2238 kebrt 58
#define PTL0_ENTRIES_ARCH    (2<<12)    // 4096
59
#define PTL1_ENTRIES_ARCH     0 
60
#define PTL2_ENTRIES_ARCH     0 
2241 kebrt 61
/* coarse page tables used (256*4 = 1KB per page) */
2238 kebrt 62
#define PTL3_ENTRIES_ARCH    (2<<8)     // 256
2128 jermar 63
 
2238 kebrt 64
#define PTL0_INDEX_ARCH(vaddr)    (((vaddr) >> 20) & 0xfff)
65
#define PTL1_INDEX_ARCH(vaddr)    0 
2241 kebrt 66
#define PTL2_INDEX_ARCH(vaddr)    0
67
/* TODO: ?? 0xfff or 0x0ff */
68
#define PTL3_INDEX_ARCH(vaddr)    (((vaddr) >> 12) & 0xfff)
2128 jermar 69
 
2238 kebrt 70
#define GET_PTL1_ADDRESS_ARCH(ptl0, i)      ((pte_t *)( (((pte_level0_t*)(ptl0))[(i)]).coarse_table_addr << 10 ))
71
#define GET_PTL2_ADDRESS_ARCH(ptl1, i)      (ptl1)
72
#define GET_PTL3_ADDRESS_ARCH(ptl2, i)      (ptl2)
73
#define GET_FRAME_ADDRESS_ARCH(ptl3, i)     ((uintptr_t)( (((pte_level1_t*)(ptl3))[(i)]).frame_base_addr << 12 ))
2128 jermar 74
 
2238 kebrt 75
#define SET_PTL0_ADDRESS_ARCH(ptl0)         (set_ptl0_addr((pte_level0_t *)(ptl0)))
76
#define SET_PTL1_ADDRESS_ARCH(ptl0, i, a)   (((pte_level0_t *)(ptl0))[(i)].coarse_table_addr = (a)>>10)
2147 jancik 77
#define SET_PTL2_ADDRESS_ARCH(ptl1, i, a)
78
#define SET_PTL3_ADDRESS_ARCH(ptl2, i, a)
2182 jancik 79
#define SET_FRAME_ADDRESS_ARCH(ptl3, i, a)  (((pte_level1_t *)(ptl3))[(i)].frame_base_addr = (a)>>12)
2128 jermar 80
 
2238 kebrt 81
#define GET_PTL1_FLAGS_ARCH(ptl0, i)        get_pt_level0_flags((pte_level0_t *)(ptl0), (index_t)(i))
82
#define GET_PTL2_FLAGS_ARCH(ptl1, i)        PAGE_PRESENT
83
#define GET_PTL3_FLAGS_ARCH(ptl2, i)        PAGE_PRESENT
84
#define GET_FRAME_FLAGS_ARCH(ptl3, i)       get_pt_level1_flags((pte_level1_t *)(ptl3), (index_t)(i))
2128 jermar 85
 
2238 kebrt 86
#define SET_PTL1_FLAGS_ARCH(ptl0, i, x)     set_pt_level0_flags((pte_level0_t *)(ptl0), (index_t)(i), (x))
2147 jancik 87
#define SET_PTL2_FLAGS_ARCH(ptl1, i, x)
88
#define SET_PTL3_FLAGS_ARCH(ptl2, i, x)
2238 kebrt 89
#define SET_FRAME_FLAGS_ARCH(ptl3, i, x)    set_pt_level1_flags((pte_level1_t *)(ptl3), (index_t)(i), (x))
2128 jermar 90
 
2182 jancik 91
#define PTE_VALID_ARCH(pte)                 (*((uint32_t *) (pte)) != 0)
2241 kebrt 92
// TODO: ?? != 0
93
#define PTE_PRESENT_ARCH(pte)               ( ((pte_level0_t *)(pte))->descriptor_type != 0 )
94
 
95
/* pte should point into ptl3 */
2238 kebrt 96
#define PTE_GET_FRAME_ARCH(pte)             ( ((pte_level1_t *)(pte))->frame_base_addr << FRAME_WIDTH)
2241 kebrt 97
/* pte should point into ptl3 */
2238 kebrt 98
#define PTE_WRITABLE_ARCH(pte)              ( ((pte_level1_t *)(pte))->access_permission_0 == pte_ap_user_rw_kernel_rw ) 
2241 kebrt 99
 
2238 kebrt 100
#define PTE_EXECUTABLE_ARCH(pte)            1
2128 jermar 101
 
102
#ifndef __ASM__
103
 
2241 kebrt 104
/**
105
 * Sets the address of level 0 page table.
106
 *
107
 * \param pt    pointer to the page table to set
2182 jancik 108
 */  
2238 kebrt 109
static inline void set_ptl0_addr( pte_level0_t* pt)
110
{
2199 jancik 111
    asm volatile ( "mcr p15, 0, %0, c2, c0, 0 \n"
2238 kebrt 112
        :
113
        : "r"(pt)
2182 jancik 114
    );
115
 
116
}
2128 jermar 117
 
2241 kebrt 118
/**
119
 * Returns level 0 page table entry flags.
120
 *
121
 * \param pt     level 0 page table
122
 * \param i      index of the entry to return
123
 */
2147 jancik 124
static inline int get_pt_level0_flags(pte_level0_t *pt, index_t i)
2128 jermar 125
{
2238 kebrt 126
    pte_level0_t *p = &pt[i];
2147 jancik 127
 
128
    return (
2149 jancik 129
        ( p->descriptor_type != pte_descriptor_not_preset ) << PAGE_PRESENT_SHIFT |
2241 kebrt 130
        ( 1 << PAGE_USER_SHIFT )  |
131
        ( 1 << PAGE_READ_SHIFT )  |
132
        ( 1 << PAGE_WRITE_SHIFT ) |
133
        ( 1 << PAGE_EXEC_SHIFT )  |
134
        ( 1 << PAGE_CACHEABLE_SHIFT  )
2147 jancik 135
    );
2238 kebrt 136
}
2147 jancik 137
 
2241 kebrt 138
/**
139
 * Returns level 1 page table entry flags.
140
 *
141
 * \param pt     level 1 page table
142
 * \param i      index of the entry to return
143
 */
2149 jancik 144
static inline int get_pt_level1_flags(pte_level1_t *pt, index_t i)
2147 jancik 145
{
2241 kebrt 146
    pte_level1_t *p = &pt[i];
2128 jermar 147
 
2147 jancik 148
    return (
2149 jancik 149
        ( p->descriptor_type != pte_descriptor_not_preset )    << PAGE_PRESENT_SHIFT |
2147 jancik 150
        ( (p->access_permission_0 == pte_ap_user_ro_kernel_rw) << PAGE_READ_SHIFT )  |
151
        ( (p->access_permission_0 == pte_ap_user_rw_kernel_rw) << PAGE_READ_SHIFT )  |
152
        ( (p->access_permission_0 == pte_ap_user_rw_kernel_rw) << PAGE_WRITE_SHIFT ) |
2182 jancik 153
        ( (p->access_permission_0 != pte_ap_user_no_kernel_rw) << PAGE_USER_SHIFT )  |
2147 jancik 154
        ( 1 << PAGE_EXEC_SHIFT ) |
2241 kebrt 155
        ( p->bufferable << PAGE_CACHEABLE )
2147 jancik 156
    );
157
}
158
 
2241 kebrt 159
/**
160
 * Sets flags of level 0 page table entry.
161
 *
162
 * \param pt     level 0 page table
163
 * \param i      index of the entry to be changed
164
 * \param flags  new flags
165
 *
166
 * TODO: why should_be_zero set to 1?
167
 */
2147 jancik 168
static inline void set_pt_level0_flags(pte_level0_t *pt, index_t i, int flags)
2128 jermar 169
{
2147 jancik 170
    pte_level0_t *p = &pt[i];
171
 
2241 kebrt 172
    if (flags & PAGE_NOT_PRESENT) {
2238 kebrt 173
        p->descriptor_type = pte_descriptor_not_preset;
174
        p->should_be_zero  = 1;
175
    } else {
176
        p->descriptor_type = pte_descriptor_coarse_table;
177
        p->should_be_zero  = 0;
2147 jancik 178
    }
2128 jermar 179
}
180
 
2241 kebrt 181
/**
182
 * Sets flags of level 1 page table entry.
183
 *
184
 * We use same access rights for the whole page. When page is not preset then
185
 * store 1 in acess_rigts_3.
186
 * TODO: why access_right_3?
187
 *
188
 * \param pt     level 1 page table
189
 * \param i      index of the entry to be changed
190
 * \param flags  new flags
2147 jancik 191
 */  
192
static inline void set_pt_level1_flags(pte_level1_t *pt, index_t i, int flags)
193
{
194
    pte_level1_t *p = &pt[i];
195
 
2241 kebrt 196
    if (flags & PAGE_NOT_PRESENT) {
2238 kebrt 197
        p->descriptor_type      = pte_descriptor_not_preset;
198
        p->access_permission_3  = 1;
199
    } else {
2241 kebrt 200
        p->descriptor_type      = pte_descriptor_small_page;
2238 kebrt 201
        p->access_permission_3  = p->access_permission_0;
202
    }
2147 jancik 203
 
2238 kebrt 204
    p->cacheable = p->bufferable = (flags & PAGE_CACHEABLE) != 0;
205
 
2241 kebrt 206
    /* default access permission */
2238 kebrt 207
    p->access_permission_0 = p->access_permission_1 =
208
        p->access_permission_2 = p->access_permission_3 = pte_ap_user_no_kernel_rw;
209
 
2241 kebrt 210
    if (flags & PAGE_USER)  {
211
        if (flags & PAGE_READ) {
2238 kebrt 212
            p->access_permission_0 = p->access_permission_1 =
213
                p->access_permission_2 = p->access_permission_3 =
214
                pte_ap_user_ro_kernel_rw;
215
        }
2241 kebrt 216
        if (flags & PAGE_WRITE) {
2238 kebrt 217
            p->access_permission_0 = p->access_permission_1 =
218
                p->access_permission_2 = p->access_permission_3 =
219
                pte_ap_user_rw_kernel_rw;
220
        }
221
    }
2147 jancik 222
}
223
 
2128 jermar 224
extern void page_arch_init(void);
225
 
226
#endif /* __ASM__ */
227
 
228
#endif /* KERNEL */
229
 
230
#endif
231
 
232
/** @}
233
 */
2147 jancik 234