1 | /////////////////////////////////////////////////////////////////////////////////// |
---|
2 | // File : vmem.c |
---|
3 | // Date : 01/07/2012 |
---|
4 | // Author : alain greiner |
---|
5 | // Copyright (c) UPMC-LIP6 |
---|
6 | /////////////////////////////////////////////////////////////////////////////////// |
---|
7 | // The vmem.c and vmem.h files are part ot the GIET-VM nano kernel. |
---|
8 | // They contain the kernel data structures and functions used to dynamically |
---|
9 | // handle the paged virtual memory. |
---|
10 | /////////////////////////////////////////////////////////////////////////////////// |
---|
11 | |
---|
12 | #include <utils.h> |
---|
13 | #include <tty_driver.h> |
---|
14 | #include <vmem.h> |
---|
15 | #include <giet_config.h> |
---|
16 | #include <tty_driver.h> |
---|
17 | |
---|
18 | ///////////////////////////////////////////////////////////////////////////// |
---|
19 | // Global variable : IOMMU page table |
---|
20 | ///////////////////////////////////////////////////////////////////////////// |
---|
21 | |
---|
22 | __attribute__((section (".iommu"))) page_table_t _iommu_ptab; |
---|
23 | |
---|
24 | ////////////////////////////////////////////////////////////////////////////// |
---|
25 | // _iommu_add_pte2() |
---|
26 | ////////////////////////////////////////////////////////////////////////////// |
---|
27 | void _iommu_add_pte2( |
---|
28 | unsigned int ix1, |
---|
29 | unsigned int ix2, |
---|
30 | unsigned int ppn, |
---|
31 | unsigned int flags) |
---|
32 | { |
---|
33 | unsigned int ptba; |
---|
34 | unsigned int * pt_ppn; |
---|
35 | unsigned int * pt_flags; |
---|
36 | |
---|
37 | // get pointer on iommu page table |
---|
38 | page_table_t * pt = &_iommu_ptab; |
---|
39 | |
---|
40 | // get ptba and update PT2 |
---|
41 | if ((pt->pt1[ix1] & PTE_V) == 0) |
---|
42 | { |
---|
43 | _tty_get_lock( 0 ); |
---|
44 | _puts("\n[GIET ERROR] in iommu_add_pte2 function\n"); |
---|
45 | _puts("the IOMMU PT1 entry is not mapped / ix1 = "); |
---|
46 | _putx( ix1 ); |
---|
47 | _puts("\n"); |
---|
48 | _tty_release_lock( 0 ); |
---|
49 | _exit(); |
---|
50 | } |
---|
51 | else |
---|
52 | { |
---|
53 | ptba = pt->pt1[ix1] << 12; |
---|
54 | pt_flags = (unsigned int *) (ptba + 8 * ix2); |
---|
55 | pt_ppn = (unsigned int *) (ptba + 8 * ix2 + 4); |
---|
56 | *pt_flags = flags; |
---|
57 | *pt_ppn = ppn; |
---|
58 | } |
---|
59 | } // end _iommu_add_pte2() |
---|
60 | |
---|
61 | |
---|
62 | ////////////////////////////////////////////////////////////////////////////// |
---|
63 | // _iommu_inval_pte2() |
---|
64 | ////////////////////////////////////////////////////////////////////////////// |
---|
65 | void _iommu_inval_pte2(unsigned int ix1, unsigned int ix2) { |
---|
66 | unsigned int ptba; |
---|
67 | unsigned int * pt_flags; |
---|
68 | |
---|
69 | // get pointer on iommu page table |
---|
70 | page_table_t * pt = &_iommu_ptab; |
---|
71 | |
---|
72 | // get ptba and inval PTE2 |
---|
73 | if ((pt->pt1[ix1] & PTE_V) == 0) |
---|
74 | { |
---|
75 | _puts("\n[GIET ERROR] in iommu_inval_pte2 function\n"); |
---|
76 | _puts("the IOMMU PT1 entry is not mapped / ix1 = "); |
---|
77 | _putx( ix1 ); |
---|
78 | _puts("\n"); |
---|
79 | _exit(); |
---|
80 | } |
---|
81 | else { |
---|
82 | ptba = pt->pt1[ix1] << 12; |
---|
83 | pt_flags = (unsigned int *) (ptba + 8 * ix2); |
---|
84 | *pt_flags = 0; |
---|
85 | } |
---|
86 | } // end _iommu_inval_pte2() |
---|
87 | |
---|
88 | ////////////////////////////////////////////////////////////////////////////// |
---|
89 | // This function makes a "vpn" to "ppn" translation, from the page table |
---|
90 | // defined by the virtual address "pt". The MMU is supposed to be activated. |
---|
91 | // It uses the address extension mechanism for physical addressing. |
---|
92 | // Return 0 if success. Return 1 if PTE1 or PTE2 unmapped. |
---|
93 | ////////////////////////////////////////////////////////////////////////////// |
---|
94 | unsigned int _v2p_translate( page_table_t* pt, |
---|
95 | unsigned int vpn, |
---|
96 | unsigned int* ppn, |
---|
97 | unsigned int* flags) |
---|
98 | { |
---|
99 | unsigned long long ptba; |
---|
100 | unsigned long long pte2_paddr; |
---|
101 | |
---|
102 | register unsigned int pte2_msb; |
---|
103 | register unsigned int pte2_lsb; |
---|
104 | register unsigned int flags_value; |
---|
105 | register unsigned int ppn_value; |
---|
106 | |
---|
107 | unsigned int ix1 = vpn >> 9; |
---|
108 | unsigned int ix2 = vpn & 0x1FF; |
---|
109 | |
---|
110 | // get PTE1 |
---|
111 | unsigned int pte1 = pt->pt1[ix1]; |
---|
112 | |
---|
113 | // check PTE1 mapping |
---|
114 | if ( (pte1 & PTE_V) == 0 ) return 1; |
---|
115 | |
---|
116 | // get physical addresses of pte2 (two 32 bits words) |
---|
117 | ptba = (unsigned long long) (pt->pt1[ix1] & 0x0FFFFFFF) << 12; |
---|
118 | pte2_paddr = ptba + 8*ix2; |
---|
119 | pte2_lsb = (unsigned int) pte2_paddr; |
---|
120 | pte2_msb = (unsigned int) (pte2_paddr >> 32); |
---|
121 | |
---|
122 | // gets ppn_value and flags_value, after temporary DTLB desactivation |
---|
123 | asm volatile ( |
---|
124 | "li $2, 0xFFFFFFFE \n" /* Mask for IE bits */ |
---|
125 | "mfc0 $4, $12 \n" /* $4 <= SR */ |
---|
126 | "and $2, $2, $4 \n" |
---|
127 | "mtc0 $2, $12 \n" /* disable Interrupts */ |
---|
128 | |
---|
129 | "li $3, 0xB \n" |
---|
130 | "mtc2 $3, $1 \n" /* DTLB unactivated */ |
---|
131 | |
---|
132 | "mtc2 %2, $24 \n" /* PADDR_EXT <= msb */ |
---|
133 | "lw %0, 0(%3) \n" /* read flags */ |
---|
134 | "lw %1, 4(%3) \n" /* read ppn */ |
---|
135 | "mtc2 $0, $24 \n" /* PADDR_EXT <= 0 */ |
---|
136 | |
---|
137 | "li $3, 0xF \n" |
---|
138 | "mtc2 $3, $1 \n" /* DTLB activated */ |
---|
139 | |
---|
140 | "mtc0 $4, $12 \n" /* restore SR */ |
---|
141 | : "=r" (flags_value), "=r" (ppn_value) |
---|
142 | : "r" (pte2_msb), "r" (pte2_lsb) |
---|
143 | : "$2","$3","$4"); |
---|
144 | |
---|
145 | // check PTE2 mapping |
---|
146 | if ( (flags_value & PTE_V) == 0 ) return 1; |
---|
147 | |
---|
148 | // set return values |
---|
149 | *ppn = ppn_value; |
---|
150 | *flags = flags_value; |
---|
151 | return 0; |
---|
152 | } // end _v2p_translate() |
---|
153 | |
---|
154 | |
---|
155 | |
---|
156 | // Local Variables: |
---|
157 | // tab-width: 4 |
---|
158 | // c-basic-offset: 4 |
---|
159 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
160 | // indent-tabs-mode: nil |
---|
161 | // End: |
---|
162 | // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|
163 | |
---|
164 | |
---|