~eliasnaur/unik

ref: cd0f505f7c2c06bac9c3655ab38b7bbe2d1761f3 unik/kernel/debug.go -rw-r--r-- 3.0 KiB
cd0f505fElias Naur initial import 6 months ago
                                                                                
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
// SPDX-License-Identifier: Unlicense OR MIT

package kernel

import (
	"fmt"
	"sort"
)

type pageTableRange struct {
	vaddr virtualAddress
	paddr physicalAddress
	size  int
}

func Verify() {
	entries := dumpPageTable(globalPT)
	verifyPageTable(entries)
}

func verifyPageTable(entries []pageTableRange) {
	type addrRange struct {
		start uint64
		end   uint64
		r     pageTableRange
	}
	var pranges []addrRange
	for _, e := range entries {
		if e.vaddr >= physicalMapOffset {
			// Ignore the identity mapped memory which overlaps
			// existing mappings per design.
			continue
		}
		pranges = append(pranges, addrRange{
			start: uint64(e.paddr),
			end:   uint64(e.paddr) + uint64(e.size),
			r:     e,
		})
	}
	overlap := false
	sort.Slice(pranges, func(i, j int) bool {
		r1, r2 := pranges[i], pranges[j]
		if r1.start < r2.start {
			return true
		}
		if r1.start == r2.start && r1.end < r2.end {
			return true
		}
		if r1.start == r2.start && r1.end == r2.end {
			overlap = true
			fmt.Printf("overlapping range: %#x %#x\n", r1.r, r2.r)
		}
		return false
	})
	for i := 0; i < len(pranges)-1; i++ {
		r1 := pranges[i]
		r2 := pranges[i+1]
		if r1.end > r2.start {
			overlap = true
			fmt.Printf("overlapping range: %#x %#x\n", r1.r, r2.r)
		}
	}
	if overlap {
		/*for _, e := range pranges {
			fmt.Printf("range: vaddr: %#x paddr: %#x size: %#x\n", e.r.vaddr, e.r.paddr, e.r.size)
		}*/
		panic("overlapping ranges")
	}
}

func dumpPageTable(pml4 *pageTable) []pageTableRange {
	var entries []pageTableRange
	for i, pml4e := range pml4 {
		if !pml4e.present() {
			continue
		}
		vaddr := virtualAddress(i * pageSizeRoot)
		// Sign extend.
		if vaddr&(maxVirtAddress>>1) != 0 {
			vaddr |= ^(maxVirtAddress - 1)
		}
		pdpt := pml4e.getPageTable()
		for i, pdpte := range pdpt {
			if !pdpte.present() {
				continue
			}
			vaddr := vaddr + virtualAddress(i)*pageSize1GB
			if pageFlags(pdpte)&pageSizeFlag != 0 {
				// 1GB page.
				paddr := physicalAddress(pdpte) & (_MAXPHYADDR - 1) &^ (pageSize1GB - 1)
				entries = append(entries, pageTableRange{vaddr, paddr, pageSize1GB})
			} else {
				pd := pdpte.getPageTable()
				for i, pde := range pd {
					if !pde.present() {
						continue
					}
					vaddr := vaddr + virtualAddress(i)*pageSize2MB
					if pageFlags(pde)&pageSizeFlag != 0 {
						// 2MB page.
						paddr := physicalAddress(pde) & (_MAXPHYADDR - 1) &^ (pageSize2MB - 1)
						entries = append(entries, pageTableRange{vaddr, paddr, pageSize2MB})
					} else {
						pt := pde.getPageTable()
						for i, e := range pt {
							if !e.present() {
								continue
							}
							vaddr := vaddr + virtualAddress(i)*pageSize
							// 4kb page.
							paddr := physicalAddress(e) & (_MAXPHYADDR - 1) &^ (pageSize - 1)
							entries = append(entries, pageTableRange{vaddr, paddr, pageSize})
						}
					}
				}
			}
		}
	}
	return entries
}

func dumpPageEntry(vaddr virtualAddress, paddr physicalAddress, size int) {
	fmt.Printf("mapping vaddr: %#x paddr: %#x size %#x\n", vaddr, paddr, size)
}