summaryrefslogtreecommitdiff
path: root/ArmPlatformPkg/Library/EblCmdLib/EblCmdMmu.c
blob: 138216e9b739237531a2b3c6f29959ff10360253 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/** @file
*
*  Copyright (c) 2011-2012, ARM Limited. All rights reserved.
*  
*  This program and the accompanying materials                          
*  are licensed and made available under the terms and conditions of the BSD License         
*  which accompanies this distribution.  The full text of the license may be found at        
*  http://opensource.org/licenses/bsd-license.php                                            
*
*  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     
*  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             
*
**/

#include <PiDxe.h>
#include <Library/UefiLib.h>
#include <Library/ArmLib.h>
#include <Chipset/ArmV7.h>
#include <Library/CacheMaintenanceLib.h>
#include <Library/EblCmdLib.h>
#include <Library/BaseLib.h>
#include <Library/DebugLib.h>

#define GET_TT_ATTRIBUTES(TTEntry)  ((TTEntry) & ~(TT_DESCRIPTOR_SECTION_BASE_ADDRESS_MASK))
#define GET_TT_PAGE_ATTRIBUTES(TTEntry)  ((TTEntry) & 0xFFF)
#define GET_TT_LARGEPAGE_ATTRIBUTES(TTEntry)  ((TTEntry) & 0xFFFF)

// Section
#define TT_DESCRIPTOR_SECTION_STRONGLY_ORDER	   (TT_DESCRIPTOR_SECTION_TYPE_SECTION 													  | \
													TT_DESCRIPTOR_SECTION_NG_GLOBAL 													  | \
													TT_DESCRIPTOR_SECTION_S_NOT_SHARED 													  | \
													TT_DESCRIPTOR_SECTION_DOMAIN(0) 													  | \
													TT_DESCRIPTOR_SECTION_AP_RW_RW 														  | \
													TT_DESCRIPTOR_SECTION_CACHE_POLICY_STRONGLY_ORDERED)

// Small Page
#define TT_DESCRIPTOR_PAGE_STRONGLY_ORDER			(TT_DESCRIPTOR_PAGE_TYPE_PAGE 														   | \
														TT_DESCRIPTOR_PAGE_NG_GLOBAL 													  | \
														TT_DESCRIPTOR_PAGE_S_NOT_SHARED 												  | \
														TT_DESCRIPTOR_PAGE_AP_RW_RW  													  | \
														TT_DESCRIPTOR_PAGE_CACHE_POLICY_STRONGLY_ORDERED)

// Large Page
#define TT_DESCRIPTOR_LARGEPAGE_WRITE_BACK              (TT_DESCRIPTOR_PAGE_TYPE_LARGEPAGE                                                           | \
                                                        TT_DESCRIPTOR_PAGE_NG_GLOBAL                                                      | \
                                                        TT_DESCRIPTOR_PAGE_S_NOT_SHARED                                                   | \
                                                        TT_DESCRIPTOR_PAGE_AP_RW_RW                                                       | \
                                                        TT_DESCRIPTOR_SECTION_CACHE_POLICY_WRITE_BACK_ALLOC)
#define TT_DESCRIPTOR_LARGEPAGE_WRITE_THROUGH           (TT_DESCRIPTOR_PAGE_TYPE_LARGEPAGE                                                           | \
                                                        TT_DESCRIPTOR_PAGE_NG_GLOBAL                                                      | \
                                                        TT_DESCRIPTOR_PAGE_S_NOT_SHARED                                                   | \
                                                        TT_DESCRIPTOR_PAGE_AP_RW_RW                                                       | \
                                                        TT_DESCRIPTOR_SECTION_CACHE_POLICY_WRITE_THROUGH_NO_ALLOC)
#define TT_DESCRIPTOR_LARGEPAGE_DEVICE                  (TT_DESCRIPTOR_PAGE_TYPE_LARGEPAGE                                                           | \
                                                        TT_DESCRIPTOR_PAGE_NG_GLOBAL                                                      | \
                                                        TT_DESCRIPTOR_PAGE_S_NOT_SHARED                                                   | \
                                                        TT_DESCRIPTOR_PAGE_AP_RW_RW                                                       | \
                                                        TT_DESCRIPTOR_SECTION_CACHE_POLICY_SHAREABLE_DEVICE)
#define TT_DESCRIPTOR_LARGEPAGE_UNCACHED                (TT_DESCRIPTOR_PAGE_TYPE_LARGEPAGE                                                           | \
                                                        TT_DESCRIPTOR_PAGE_NG_GLOBAL                                                      | \
                                                        TT_DESCRIPTOR_PAGE_S_NOT_SHARED                                                   | \
                                                        TT_DESCRIPTOR_PAGE_AP_RW_RW                                                       | \
                                                        TT_DESCRIPTOR_SECTION_CACHE_POLICY_NON_CACHEABLE)


typedef enum { Level0, Level1,Level2 } MMU_LEVEL;

typedef struct {
  MMU_LEVEL   Level;
  UINT32      Value;
  UINT32      Index;
  UINT32*     Table;
} MMU_ENTRY;

MMU_ENTRY
MmuEntryCreate (
  IN MMU_LEVEL Level,
  IN UINT32* Table,
  IN UINT32 Index
  )
{
  MMU_ENTRY Entry;
  Entry.Level = Level;
  Entry.Value = Table[Index];
  Entry.Table = Table;
  Entry.Index = Index;
  return Entry;
}

UINT32
MmuEntryIsValidAddress (
  IN MMU_LEVEL Level,
  IN UINT32 Entry
  )
{
  if (Level == Level0) {
    return 0;
  } else if (Level == Level1) {
    if ((Entry & 0x3) == 0) {           // Ignored
      return 0;
    } else if ((Entry & 0x3) == 2) {    // Section Type
      return 1;
    } else {                            // Page Type
      return 0;
    }
  } else if (Level == Level2){
    if ((Entry & 0x3) == 0) {           // Ignored
      return 0;
    } else {                            // Page Type
      return 1;
    }
  } else {
    DEBUG((EFI_D_ERROR,"MmuEntryIsValidAddress: Level:%d Entry:0x%X\n",(UINT32)Level,(UINT32)Entry));
    ASSERT(0);
    return 0;
  }
}

UINT32
MmuEntryGetAddress (
  IN MMU_ENTRY Entry
  )
{
  if (Entry.Level == Level1) {
    if ((Entry.Value & 0x3) == 0) {
      return 0;
    } else if ((Entry.Value & 0x3) == 2) {    // Section Type
      return Entry.Value & TT_DESCRIPTOR_SECTION_BASE_ADDRESS_MASK;
    } else if ((Entry.Value & 0x3) == 1) {    // Level2 Table
      MMU_ENTRY Level2Entry = MmuEntryCreate (Level2,(UINT32*)(Entry.Value & 0xFFFFC000),0);
      return MmuEntryGetAddress (Level2Entry);
    } else {                                  // Page Type
      return 0;
    }
  } else if (Entry.Level == Level2) {
    if ((Entry.Value & 0x3) == 0) {           // Ignored
      return 0;
    } else if ((Entry.Value & 0x3) == 1) {    // Large Page
      return Entry.Value & 0xFFFF0000;
    } else if ((Entry.Value & 0x2) == 2) {    // Small Page
      return Entry.Value & 0xFFFFF000;
    } else {
      return 0;
    }
  } else {
    ASSERT(0);
    return 0;
  }
}

UINT32
MmuEntryGetSize (
  IN MMU_ENTRY Entry
  )
{
  if (Entry.Level == Level1) {
    if ((Entry.Value & 0x3) == 0) {
      return 0;
    } else if ((Entry.Value & 0x3) == 2) {
      if (Entry.Value & (1 << 18))
        return 16*SIZE_1MB;
      else
        return SIZE_1MB;
    } else if ((Entry.Value & 0x3) == 1) {      // Level2 Table split 1MB section
      return SIZE_1MB;
    } else {
      DEBUG((EFI_D_ERROR, "MmuEntryGetSize: Value:0x%X",Entry.Value));
      ASSERT(0);
      return 0;
    }
  } else if (Entry.Level == Level2) {
    if ((Entry.Value & 0x3) == 0) {           // Ignored
      return 0;
    } else if ((Entry.Value & 0x3) == 1) {    // Large Page
      return SIZE_64KB;
    } else if ((Entry.Value & 0x2) == 2) {    // Small Page
      return SIZE_4KB;
    } else {
      ASSERT(0);
      return 0;
    }
  } else {
    ASSERT(0);
    return 0;
  }
}

CONST CHAR8*
MmuEntryGetAttributesName (
  IN MMU_ENTRY Entry
  )
{
  UINT32 Value;

  if (Entry.Level == Level1) {
    Value = GET_TT_ATTRIBUTES(Entry.Value) | TT_DESCRIPTOR_SECTION_NS_MASK;
    if (Value == TT_DESCRIPTOR_SECTION_WRITE_BACK(0))
      return "TT_DESCRIPTOR_SECTION_WRITE_BACK";
    else if (Value == TT_DESCRIPTOR_SECTION_WRITE_THROUGH(0))
      return "TT_DESCRIPTOR_SECTION_WRITE_THROUGH";
    else if (Value == TT_DESCRIPTOR_SECTION_DEVICE(0))
      return "TT_DESCRIPTOR_SECTION_DEVICE";
    else if (Value == TT_DESCRIPTOR_SECTION_UNCACHED(0))
      return "TT_DESCRIPTOR_SECTION_UNCACHED";
    else if (Value == TT_DESCRIPTOR_SECTION_STRONGLY_ORDER)
      return "TT_DESCRIPTOR_SECTION_STRONGLY_ORDERED";
    else {
      return "SectionUnknown";
    }
  } else if ((Entry.Level == Level2) && ((Entry.Value & 0x2) == 2)) { //Small Page
    Value = GET_TT_PAGE_ATTRIBUTES(Entry.Value);
    if (Value == TT_DESCRIPTOR_PAGE_WRITE_BACK)
      return "TT_DESCRIPTOR_PAGE_WRITE_BACK";
    else if (Value == TT_DESCRIPTOR_PAGE_WRITE_THROUGH)
      return "TT_DESCRIPTOR_PAGE_WRITE_THROUGH";
    else if (Value == TT_DESCRIPTOR_PAGE_DEVICE)
      return "TT_DESCRIPTOR_PAGE_DEVICE";
    else if (Value == TT_DESCRIPTOR_PAGE_UNCACHED)
      return "TT_DESCRIPTOR_PAGE_UNCACHED";
    else if (Value == TT_DESCRIPTOR_PAGE_STRONGLY_ORDER)
      return "TT_DESCRIPTOR_PAGE_STRONGLY_ORDERED";
    else {
      return "PageUnknown";
    }
  } else if ((Entry.Level == Level2) && ((Entry.Value & 0x3) == 1)) { //Large Page
    Value = GET_TT_LARGEPAGE_ATTRIBUTES(Entry.Value);
    if (Value == TT_DESCRIPTOR_LARGEPAGE_WRITE_BACK)
      return "TT_DESCRIPTOR_LARGEPAGE_WRITE_BACK";
    else if (Value == TT_DESCRIPTOR_LARGEPAGE_WRITE_THROUGH)
      return "TT_DESCRIPTOR_LARGEPAGE_WRITE_THROUGH";
    else if (Value == TT_DESCRIPTOR_LARGEPAGE_DEVICE)
      return "TT_DESCRIPTOR_LARGEPAGE_DEVICE";
    else if (Value == TT_DESCRIPTOR_LARGEPAGE_UNCACHED)
      return "TT_DESCRIPTOR_LARGEPAGE_UNCACHED";
    else {
      return "LargePageUnknown";
    }
  } else {
    ASSERT(0);
    return "";
  }
}

UINT32
MmuEntryGetAttributes (
  IN MMU_ENTRY Entry
  )
{
  if (Entry.Level == Level1) {
    if ((Entry.Value & 0x3) == 0) {
      return 0;
    } else if ((Entry.Value & 0x3) == 2) {
      return GET_TT_ATTRIBUTES(Entry.Value);
    } else {
      return 0;
    }
  } else if ((Entry.Level == Level2) && ((Entry.Value & 0x2) == 2)) { //Small Page
    if (GET_TT_PAGE_ATTRIBUTES(Entry.Value) == TT_DESCRIPTOR_PAGE_WRITE_BACK)
      return TT_DESCRIPTOR_SECTION_WRITE_BACK(0);
    else if (GET_TT_PAGE_ATTRIBUTES(Entry.Value) == TT_DESCRIPTOR_PAGE_WRITE_THROUGH)
      return TT_DESCRIPTOR_SECTION_WRITE_THROUGH(0);
    else if (GET_TT_PAGE_ATTRIBUTES(Entry.Value) == TT_DESCRIPTOR_PAGE_DEVICE)
      return TT_DESCRIPTOR_SECTION_DEVICE(0);
    else if (GET_TT_PAGE_ATTRIBUTES(Entry.Value) == TT_DESCRIPTOR_PAGE_UNCACHED)
      return TT_DESCRIPTOR_SECTION_UNCACHED(0);
    else if (GET_TT_PAGE_ATTRIBUTES(Entry.Value) == TT_DESCRIPTOR_PAGE_STRONGLY_ORDER)
      return TT_DESCRIPTOR_SECTION_STRONGLY_ORDER;
    else {
      return 0;
    }
  } else if ((Entry.Level == Level2) && ((Entry.Value & 0x3) == 1)) { //Large Page
    if (GET_TT_LARGEPAGE_ATTRIBUTES(Entry.Value) == TT_DESCRIPTOR_LARGEPAGE_WRITE_BACK)
      return TT_DESCRIPTOR_SECTION_WRITE_BACK(0);
    else if (GET_TT_LARGEPAGE_ATTRIBUTES(Entry.Value) == TT_DESCRIPTOR_LARGEPAGE_WRITE_THROUGH)
      return TT_DESCRIPTOR_SECTION_WRITE_THROUGH(0);
    else if (GET_TT_LARGEPAGE_ATTRIBUTES(Entry.Value) == TT_DESCRIPTOR_LARGEPAGE_DEVICE)
      return TT_DESCRIPTOR_SECTION_DEVICE(0);
    else if (GET_TT_LARGEPAGE_ATTRIBUTES(Entry.Value) == TT_DESCRIPTOR_LARGEPAGE_UNCACHED)
      return TT_DESCRIPTOR_SECTION_UNCACHED(0);
    else {
      return 0;
    }
  } else {
    return 0;
  }
}


MMU_ENTRY
DumpMmuLevel (
  IN MMU_LEVEL Level,
  IN UINT32* Table,
  IN MMU_ENTRY PreviousEntry
  )
{
  UINT32      Index = 0, Count;
  MMU_ENTRY   LastEntry, Entry;

	ASSERT((Level == Level1) || (Level == Level2));

  if (Level == Level1)    Count = 4096;
  else                    Count = 256;

  // At Level1, we will get into this function because PreviousEntry is not valid
  if (!MmuEntryIsValidAddress((MMU_LEVEL)(Level-1),PreviousEntry.Value)) {
      // Find the first valid address
      for (; (Index < Count) && (!MmuEntryIsValidAddress(Level,Table[Index])); Index++);

      LastEntry = MmuEntryCreate(Level,Table,Index);
      Index++;
  } else {
    LastEntry = PreviousEntry;
  }

  for (; Index < Count; Index++) {
    Entry = MmuEntryCreate(Level,Table,Index);
    if ((Level == Level1) && ((Entry.Value & 0x3) == 1)) {       // We have got a Level2 table redirection
      LastEntry = DumpMmuLevel(Level2,(UINT32*)(Entry.Value & 0xFFFFFC00),LastEntry);
    } else if (!MmuEntryIsValidAddress(Level,Table[Index])) {
      if (MmuEntryIsValidAddress(LastEntry.Level,LastEntry.Value)) {
          AsciiPrint("0x%08X-0x%08X\t%a\n",
              MmuEntryGetAddress(LastEntry),MmuEntryGetAddress(PreviousEntry)+MmuEntryGetSize(PreviousEntry)-1,
              MmuEntryGetAttributesName(LastEntry));
      }
      LastEntry = Entry;
    } else {
      if (MmuEntryGetAttributes(LastEntry) != MmuEntryGetAttributes(Entry)) {
          if (MmuEntryIsValidAddress(Level,LastEntry.Value)) {
            AsciiPrint("0x%08X-0x%08X\t%a\n",
                      MmuEntryGetAddress(LastEntry),MmuEntryGetAddress(PreviousEntry)+MmuEntryGetSize(PreviousEntry)-1,
                      MmuEntryGetAttributesName(LastEntry));
          }
          LastEntry = Entry;
      } else {
        ASSERT(LastEntry.Value != 0);
      }
    }
    PreviousEntry = Entry;
  }

  if ((Level == Level1) && (LastEntry.Index != Index) && MmuEntryIsValidAddress(Level,LastEntry.Value)) {
    AsciiPrint("0x%08X-0x%08X\t%a\n",
                  MmuEntryGetAddress(LastEntry),MmuEntryGetAddress(PreviousEntry)+MmuEntryGetSize(PreviousEntry)-1,
                  MmuEntryGetAttributesName(LastEntry));
  }

  return LastEntry;
}


EFI_STATUS
EblDumpMmu (
  IN UINTN  Argc,
  IN CHAR8  **Argv
  )
{
  UINT32  *TTEntry;
  MMU_ENTRY NoEntry;

  TTEntry = ArmGetTTBR0BaseAddress();

  AsciiPrint ("\nTranslation Table:0x%X\n",TTEntry);
  AsciiPrint ("Address Range\t\tAttributes\n");
  AsciiPrint ("____________________________________________________\n");

  NoEntry.Level = (MMU_LEVEL)200;
  DumpMmuLevel(Level1,TTEntry,NoEntry);

  return EFI_SUCCESS;
}