Rev 793 | Rev 826 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 793 | Rev 814 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (C) 2006 Jakub Jermar |
2 | * Copyright (C) 2006 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | #include <genarch/mm/page_pt.h> |
29 | #include <genarch/mm/page_pt.h> |
30 | #include <mm/page.h> |
30 | #include <mm/page.h> |
31 | #include <mm/frame.h> |
31 | #include <mm/frame.h> |
32 | #include <mm/as.h> |
32 | #include <mm/as.h> |
33 | #include <arch/mm/page.h> |
33 | #include <arch/mm/page.h> |
34 | #include <arch/mm/as.h> |
34 | #include <arch/mm/as.h> |
35 | #include <arch/types.h> |
35 | #include <arch/types.h> |
36 | #include <typedefs.h> |
36 | #include <typedefs.h> |
37 | #include <arch/asm.h> |
37 | #include <arch/asm.h> |
38 | #include <memstr.h> |
38 | #include <memstr.h> |
39 | 39 | ||
40 | static void pt_mapping_insert(as_t *as, __address page, __address frame, int flags); |
40 | static void pt_mapping_insert(as_t *as, __address page, __address frame, int flags); |
41 | static pte_t *pt_mapping_find(as_t *as, __address page); |
41 | static pte_t *pt_mapping_find(as_t *as, __address page); |
42 | 42 | ||
43 | page_mapping_operations_t pt_mapping_operations = { |
43 | page_mapping_operations_t pt_mapping_operations = { |
44 | .mapping_insert = pt_mapping_insert, |
44 | .mapping_insert = pt_mapping_insert, |
45 | .mapping_find = pt_mapping_find |
45 | .mapping_find = pt_mapping_find |
46 | }; |
46 | }; |
47 | 47 | ||
48 | /** Map page to frame using hierarchical page tables. |
48 | /** Map page to frame using hierarchical page tables. |
49 | * |
49 | * |
50 | * Map virtual address 'page' to physical address 'frame' |
50 | * Map virtual address 'page' to physical address 'frame' |
51 | * using 'flags'. |
51 | * using 'flags'. |
52 | * |
52 | * |
53 | * The address space must be locked and interrupts must be disabled. |
53 | * The address space must be locked and interrupts must be disabled. |
54 | * |
54 | * |
55 | * @param as Address space to wich page belongs. |
55 | * @param as Address space to wich page belongs. |
56 | * @param page Virtual address of the page to be mapped. |
56 | * @param page Virtual address of the page to be mapped. |
57 | * @param frame Physical address of memory frame to which the mapping is done. |
57 | * @param frame Physical address of memory frame to which the mapping is done. |
58 | * @param flags Flags to be used for mapping. |
58 | * @param flags Flags to be used for mapping. |
59 | */ |
59 | */ |
60 | void pt_mapping_insert(as_t *as, __address page, __address frame, int flags) |
60 | void pt_mapping_insert(as_t *as, __address page, __address frame, int flags) |
61 | { |
61 | { |
62 | pte_t *ptl0, *ptl1, *ptl2, *ptl3; |
62 | pte_t *ptl0, *ptl1, *ptl2, *ptl3; |
63 | __address newpt; |
63 | __address newpt; |
64 | 64 | ||
65 | ptl0 = (pte_t *) PA2KA((__address) as->page_table); |
65 | ptl0 = (pte_t *) PA2KA((__address) as->page_table); |
66 | 66 | ||
67 | if (GET_PTL1_FLAGS(ptl0, PTL0_INDEX(page)) & PAGE_NOT_PRESENT) { |
67 | if (GET_PTL1_FLAGS(ptl0, PTL0_INDEX(page)) & PAGE_NOT_PRESENT) { |
68 | newpt = frame_alloc(ONE_FRAME, FRAME_KA); |
68 | newpt = PA2KA(PFN2ADDR(frame_alloc(ONE_FRAME, FRAME_KA))); |
69 | memsetb(newpt, PAGE_SIZE, 0); |
69 | memsetb(newpt, PAGE_SIZE, 0); |
70 | SET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page), KA2PA(newpt)); |
70 | SET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page), KA2PA(newpt)); |
71 | SET_PTL1_FLAGS(ptl0, PTL0_INDEX(page), PAGE_PRESENT | PAGE_USER | PAGE_EXEC | PAGE_CACHEABLE | PAGE_WRITE); |
71 | SET_PTL1_FLAGS(ptl0, PTL0_INDEX(page), PAGE_PRESENT | PAGE_USER | PAGE_EXEC | PAGE_CACHEABLE | PAGE_WRITE); |
72 | } |
72 | } |
73 | 73 | ||
74 | ptl1 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page))); |
74 | ptl1 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page))); |
75 | 75 | ||
76 | if (GET_PTL2_FLAGS(ptl1, PTL1_INDEX(page)) & PAGE_NOT_PRESENT) { |
76 | if (GET_PTL2_FLAGS(ptl1, PTL1_INDEX(page)) & PAGE_NOT_PRESENT) { |
77 | newpt = frame_alloc(ONE_FRAME, FRAME_KA); |
77 | newpt = PA2KA(PFN2ADDR(frame_alloc(ONE_FRAME, FRAME_KA))); |
78 | memsetb(newpt, PAGE_SIZE, 0); |
78 | memsetb(newpt, PAGE_SIZE, 0); |
79 | SET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page), KA2PA(newpt)); |
79 | SET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page), KA2PA(newpt)); |
80 | SET_PTL2_FLAGS(ptl1, PTL1_INDEX(page), PAGE_PRESENT | PAGE_USER | PAGE_EXEC | PAGE_CACHEABLE | PAGE_WRITE); |
80 | SET_PTL2_FLAGS(ptl1, PTL1_INDEX(page), PAGE_PRESENT | PAGE_USER | PAGE_EXEC | PAGE_CACHEABLE | PAGE_WRITE); |
81 | } |
81 | } |
82 | 82 | ||
83 | ptl2 = (pte_t *) PA2KA(GET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page))); |
83 | ptl2 = (pte_t *) PA2KA(GET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page))); |
84 | 84 | ||
85 | if (GET_PTL3_FLAGS(ptl2, PTL2_INDEX(page)) & PAGE_NOT_PRESENT) { |
85 | if (GET_PTL3_FLAGS(ptl2, PTL2_INDEX(page)) & PAGE_NOT_PRESENT) { |
86 | newpt = frame_alloc(ONE_FRAME, FRAME_KA); |
86 | newpt = PA2KA(PFN2ADDR(frame_alloc(ONE_FRAME, FRAME_KA))); |
87 | memsetb(newpt, PAGE_SIZE, 0); |
87 | memsetb(newpt, PAGE_SIZE, 0); |
88 | SET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page), KA2PA(newpt)); |
88 | SET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page), KA2PA(newpt)); |
89 | SET_PTL3_FLAGS(ptl2, PTL2_INDEX(page), PAGE_PRESENT | PAGE_USER | PAGE_EXEC | PAGE_CACHEABLE | PAGE_WRITE); |
89 | SET_PTL3_FLAGS(ptl2, PTL2_INDEX(page), PAGE_PRESENT | PAGE_USER | PAGE_EXEC | PAGE_CACHEABLE | PAGE_WRITE); |
90 | } |
90 | } |
91 | 91 | ||
92 | ptl3 = (pte_t *) PA2KA(GET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page))); |
92 | ptl3 = (pte_t *) PA2KA(GET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page))); |
93 | 93 | ||
94 | SET_FRAME_ADDRESS(ptl3, PTL3_INDEX(page), frame); |
94 | SET_FRAME_ADDRESS(ptl3, PTL3_INDEX(page), frame); |
95 | SET_FRAME_FLAGS(ptl3, PTL3_INDEX(page), flags); |
95 | SET_FRAME_FLAGS(ptl3, PTL3_INDEX(page), flags); |
96 | } |
96 | } |
97 | 97 | ||
98 | /** Find mapping for virtual page in hierarchical page tables. |
98 | /** Find mapping for virtual page in hierarchical page tables. |
99 | * |
99 | * |
100 | * Find mapping for virtual page. |
100 | * Find mapping for virtual page. |
101 | * |
101 | * |
102 | * The address space must be locked and interrupts must be disabled. |
102 | * The address space must be locked and interrupts must be disabled. |
103 | * |
103 | * |
104 | * @param as Address space to which page belongs. |
104 | * @param as Address space to which page belongs. |
105 | * @param page Virtual page. |
105 | * @param page Virtual page. |
106 | * |
106 | * |
107 | * @return NULL if there is no such mapping; entry from PTL3 describing the mapping otherwise. |
107 | * @return NULL if there is no such mapping; entry from PTL3 describing the mapping otherwise. |
108 | */ |
108 | */ |
109 | pte_t *pt_mapping_find(as_t *as, __address page) |
109 | pte_t *pt_mapping_find(as_t *as, __address page) |
110 | { |
110 | { |
111 | pte_t *ptl0, *ptl1, *ptl2, *ptl3; |
111 | pte_t *ptl0, *ptl1, *ptl2, *ptl3; |
112 | 112 | ||
113 | ptl0 = (pte_t *) PA2KA((__address) as->page_table); |
113 | ptl0 = (pte_t *) PA2KA((__address) as->page_table); |
114 | 114 | ||
115 | if (GET_PTL1_FLAGS(ptl0, PTL0_INDEX(page)) & PAGE_NOT_PRESENT) |
115 | if (GET_PTL1_FLAGS(ptl0, PTL0_INDEX(page)) & PAGE_NOT_PRESENT) |
116 | return NULL; |
116 | return NULL; |
117 | 117 | ||
118 | ptl1 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page))); |
118 | ptl1 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page))); |
119 | 119 | ||
120 | if (GET_PTL2_FLAGS(ptl1, PTL1_INDEX(page)) & PAGE_NOT_PRESENT) |
120 | if (GET_PTL2_FLAGS(ptl1, PTL1_INDEX(page)) & PAGE_NOT_PRESENT) |
121 | return NULL; |
121 | return NULL; |
122 | 122 | ||
123 | ptl2 = (pte_t *) PA2KA(GET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page))); |
123 | ptl2 = (pte_t *) PA2KA(GET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page))); |
124 | 124 | ||
125 | if (GET_PTL3_FLAGS(ptl2, PTL2_INDEX(page)) & PAGE_NOT_PRESENT) |
125 | if (GET_PTL3_FLAGS(ptl2, PTL2_INDEX(page)) & PAGE_NOT_PRESENT) |
126 | return NULL; |
126 | return NULL; |
127 | 127 | ||
128 | ptl3 = (pte_t *) PA2KA(GET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page))); |
128 | ptl3 = (pte_t *) PA2KA(GET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page))); |
129 | 129 | ||
130 | return &ptl3[PTL3_INDEX(page)]; |
130 | return &ptl3[PTL3_INDEX(page)]; |
131 | } |
131 | } |
132 | 132 |