aboutsummaryrefslogtreecommitdiffstats
path: root/bootloader/paging.c
blob: d3d6dba547db0958907fb2d999f4e0b1deb76386 (plain) (blame)
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
#include <efi.h>
#include <efilib.h>
#include <stdint.h>

#include "memorytypes.h"
#include "paging_struct.h"
#include "memory.h"

uint64_t size;
uint64_t baseaddr;
uint64_t current;

uint64_t count;

EFI_PHYSICAL_ADDRESS pages;
uint64_t * CR3;

uint64_t virtualmemory;
uint64_t maxneg;
//uint64_t base_memory;

uint64_t GetNextEntry()
{
  uint64_t ret = current + 0x1000;//1024*4;

  count++;

  if(count > 1023)
  {
    // need to allocate new page
    // 1024 pages for 4MB
    EFI_STATUS allocstatus = uefi_call_wrapper(BS->AllocatePages, 4, AllocateAnyPages, MEM_PAGING, size, &pages);
    if(allocstatus != EFI_SUCCESS)
    {
      Print(L"Paging space allocation failed\n");
      Print(L"Current: 0x%llX \n", current);
      return -1;
    }
    bootloader_memset((void*)pages, size*0x1000, 0x0);
    baseaddr = (uint64_t)pages;
    ret = baseaddr;

    //Print(L"New Chunk: 0x%llX\n", baseaddr);
    count = 0;

    current = ret;

    if(ret % 0x1000 != 0)
    {
      Print(L"WARNING Memory not aligned\n");
    }

    return ret;
  }

  //Print(L"New Entry: 0x%llX\n", ret);
  current = ret;

  if(ret % 0x1000 != 0)
  {
    Print(L"WARNING Memory not aligned\n");
  }

  return ret;
}

uint8_t NeedAllocation(uint64_t in)
{
  if(in == -1)
  {
    return 1;
  }

  return 0;
}

UINT64 EFIAPI GetVMCPUID()
{
  long out = 0;
  long id = 0x80000008;

  __asm__ __volatile__ ("movq %1, %%rax;"
                        "cpuid;"
                        "movq %%rax, %0;"
                        :"=r"(out)
                        :"r"(id)
                        );

  return out;
}

uint64_t powerTwo(uint64_t power)
{
  uint64_t ret = 1;

  while(power > 0)
  {
    ret *= 2;

    power--;
  }

  return ret;
}

void initCR3()
{
  UINT64 cpu = GetVMCPUID();

  CPUIDsizes * sizes = (CPUIDsizes*)&cpu;

  // Print(L"raw: %x\n", cpu);
  // Print(L"Physical: %d\n", sizes->PhysicalAddress);
  // Print(L"Virtual: %d\n", sizes->VirtualAddress);
  virtualmemory = sizes->PhysicalAddress;

  maxneg = powerTwo(virtualmemory) - 1;

  size = 1024;

  Print(L"Started CR3\n");

  // 1024 pages for 4MB
  EFI_STATUS allocstatus = uefi_call_wrapper(BS->AllocatePages, 4, AllocateAnyPages, MEM_PAGING, size, &pages);
  if(allocstatus != EFI_SUCCESS)
  {
    Print(L"Paging space allocation failed\n");
    return;
  }

  count = 1;

  // 1024 pages at 4kb each
  bootloader_memset((void*)pages, size*4*1024, 0x0);

  baseaddr = (uint64_t)pages;
  current = baseaddr;

  CR3 = (uint64_t*)current;
  *CR3 = 0;

  *CR3 |= CR3_PCD;
  *CR3 |= CR3_PWT;
  //*CR3 |=(maxneg << CR3_ADDR_SHIFT);

  //((s_CR3*)CR3)->PCD = 1;
  //((s_CR3*)CR3)->PWT = 1;
  //((s_CR3*)CR3)->base_addr = GetNextEntry();


  //int32_t pml4es = sizeof(s_PML4E);


  //((s_CR3*)CR3)->base_addr = maxneg;

}

void printCR3()
{
  Print(L"CR3 value: 0x%llX\n", *((uint64_t*)CR3));
}

void writeCR3()
{
  __asm__ __volatile__("movq %0, %%cr3;"
          :
          :"r"(*CR3));
}

void SetVirtualAddress(uint64_t phy, uint64_t virt)
{
  uint64_t* pml4 = (uint64_t*)MaskPhyAddr(*CR3);

    // Get the offsets to each page table
    uint64_t pml4offset = GetPML4Offset(virt);
    uint64_t pdpoffset = GetPDPOffset(virt);
    uint64_t pdoffset = GetPDOffset(virt);
    uint64_t ptoffset = GetPTOffset(virt);


    uint64_t pml4e = pml4[pml4offset];

    // if not present, allocate the page
    if((pml4e & PE_P) == 0x0ull)
    {
      //assign the physical addr, and clear all to zero
      uint64_t tmpaddr = GetNextEntry();
      pml4e |= MaskPhyAddr(tmpaddr);

      // initialize the page
      pml4e |= PE_P;
      pml4e |= PE_RW;
      pml4e |= PE_US;
      pml4e |= PE_PWT;
      pml4e |= PE_PCD;
    }
    // assign the values to the array
    pml4[pml4offset] = pml4e;


    uint64_t pdpe = ((uint64_t*)MaskPhyAddr(pml4e))[pdpoffset];
    if((pdpe & PE_P) == 0x0ull)
    {
      // page not present
      uint64_t tmpaddr = GetNextEntry();
      pdpe |= MaskPhyAddr(tmpaddr);

      pdpe |= PE_P;
      pdpe |= PE_RW;
      pdpe |= PE_US;
      pdpe |= PE_PWT;
      pdpe |= PE_PCD;
    }
    ((uint64_t*)MaskPhyAddr(pml4e))[pdpoffset] = pdpe;

    uint64_t pde = ((uint64_t*)MaskPhyAddr(pdpe))[pdoffset];

    if((pde & PE_P) == 0x0ull)
    {
      uint64_t tmpaddr = GetNextEntry();

      pde |= MaskPhyAddr(tmpaddr);

      pde |= PE_P;
      pde |= PE_RW;
      pde |= PE_US;
      pde |= PE_PWT;
      pde |= PE_PCD;
    }
    ((uint64_t*)MaskPhyAddr(pdpe))[pdoffset] = pde;

    uint64_t pte = ((uint64_t*)MaskPhyAddr(pde))[ptoffset];

    // need to make the pt entry

    if((pte & PE_P) == 0x0ull)
    {
      pte |= PE_P;
      pte |= PE_RW;
      pte |= PE_US;
      pte |= PE_PWT;
      pte |= PE_PCD;
      pte |= MaskPhyAddr(phy);
    }
    ((uint64_t*)MaskPhyAddr(pde))[ptoffset] = pte;
}

uint8_t checkIdentity(uint64_t phy)
{
  //uint8_t phyvalue = (uint8_t)(*(uint64_t*)phy);
  uint8_t phyvalue = *(uint64_t*)phy;
  uint8_t virtvalue;
  uint64_t pml4e = CR3GetAddr(*CR3);// + GetPML4Offset(phy);
  pml4e = *(((uint64_t*)pml4e) + GetPML4Offset(phy));
  uint64_t pdpe = *(((uint64_t*)GetAddr(pml4e)) + GetPDPOffset(phy));
  uint64_t pde  = *(((uint64_t*)GetAddr(pdpe)) + GetPDOffset(phy));
  uint64_t pte  = *(((uint64_t*)GetAddr(pde)) + GetPTOffset(phy));
  uint64_t pteptr = ((GetAddr(pte)) + GetPhyOffset(phy));
  virtvalue = *(uint64_t*)((GetAddr(pte)) + GetPhyOffset(phy));

  //s_PDPE * pdpe = ((s_PDPE*)(pml4e->PDPBA + 8*virt->PDP));
  //s_PDE * pde = ((s_PDE*)(pdpe->PDBA + 8*virt->PD));
  //s_PTE * pte = ((s_PTE*)(pde->PTBA + 8*virt->PT));
  //virtvalue = *(uint8_t*)(pte->PPBA + virt->offset);

  // get the correct virtual value

  Print(L"ADDR: 0x%llx\n", phy);

  Print(L"PML4E 0x%llx\n", pml4e);
  Print(L"PDPE  0x%llx\n", pdpe);
  Print(L"PDE   0x%llx\n", pde);
  Print(L"PTE   0x%llx\n", pte);
  Print(L"pageentry: 0x%llx\n", pteptr);
  Print(L"PPBA  0x%llx\n", GetAddr(pte));

  Print(L"PML4 %d\n", GetPML4Offset(phy));
  Print(L"PDP  %d\n", GetPDPOffset(phy));
  Print(L"PD   %d\n", GetPDOffset(phy));
  Print(L"PT   %d\n", GetPTOffset(phy));
  Print(L"PHY  0x%llx\n", GetPhyOffset(phy));


  Print(L"PHYSICAL: 0x%x\n", phyvalue);
  Print(L"VIRTUAL: 0x%x\n", virtvalue);

  if(phyvalue != virtvalue)
  {
    Print(L"Incorrect physical virtual value\n");
    return 0;
  }

  Print(L"Correct physical virtual value\n");
  return 1;
}