summaryrefslogtreecommitdiffstats
path: root/src/kernel/vmmmgr.C
blob: 71fb3352318c7567f4882be84e240c3e76320044 (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
#include <util/singleton.H>
#include <kernel/vmmmgr.H>
#include <kernel/console.H>
#include <kernel/ppcarch.H>

extern void* data_load_address;

VmmManager::VmmManager() : lock()
{
}

void VmmManager::init()
{
    printk("Starting VMM...");
    
    VmmManager& v = Singleton<VmmManager>::instance();

    v.initSLB();
    v.initPTEs();
    v.initSDR1();

    printk("done.\n");
};

void VmmManager::init_slb()
{
    VmmManager& v = Singleton<VmmManager>::instance();
    v.initSLB();
    v.initSDR1();
}

bool VmmManager::pteMiss(task_t* t)
{
    return Singleton<VmmManager>::instance()._pteMiss(t);
}

void* VmmManager::mmioMap(void* ra, size_t pages)
{
    return Singleton<VmmManager>::instance()._mmioMap(ra,pages);
}

int VmmManager::mmioUnmap(void* ea, size_t pages)
{
    return Singleton<VmmManager>::instance()._mmioUnmap(ea,pages);
}

void VmmManager::initSLB()
{
    register uint64_t slbRS, slbRB;

    // ESID = 0, V = 1, Index = 1.
    slbRB = 0x0000000008000001; 
    
    // B = 01 (1TB), VSID = 0, Ks = 0, Kp = 1, NLCLP = 0
    slbRS = 0x4000000000000400;
    
    asm volatile("slbmte %0, %1" :: "r"(slbRS), "r"(slbRB) : "memory");
    asm volatile("isync" ::: "memory");
}

void VmmManager::initPTEs()
{
    // Invalidate all.
    for(int i = 0; i < PTEG_COUNT; i++)
	for (int j = 0; j < PTEG_SIZE; j++)
	    setValid(false, getPte(i,j));
    
    // Set up linear map.
    for(int i = 0; i < (FULL_MEM_SIZE / PAGESIZE); i++)
    {
	ACCESS_TYPES access = NORMAL_ACCESS;
	if (0 == i)
	{
	    access = NO_USER_ACCESS;
	}
	else if (((uint64_t)&data_load_address) > (i * PAGESIZE))
	{
	    access = READ_O_ACCESS;
	}
	volatile pte_t& pte = getPte(i, 0);
	defaultPte(pte);
	setTid(LinearSpace, pte);
	setAccess(access, pte);
	setPage(i, pte);
	setValid(true, pte);
    }
}

void VmmManager::initSDR1()
{
    // HTABORG, HTABSIZE = 0 (11 bits, 256k table)
    register uint64_t sdr1 = (uint64_t)HTABORG;
    asm volatile("mtsdr1 %0" :: "r"(sdr1) : "memory");
}

VmmManager::pte_t* VmmManager::page_table 
	= (VmmManager::pte_t*) HTABORG;

bool VmmManager::_pteMiss(task_t* t)
{
    lock.lock();

    uint64_t effAddr = ppc_getDAR();
    uint64_t effPid = effAddr / FULL_MEM_SIZE;

    
    if (effPid == LinearSpace)
    {
	lock.unlock();
	return false;	// Should not get this exception in Linear space
			// because it is all mapped in all the time.
    }
    
    // Check for exception in MMIO vs Dynamic Stack space.
    if (effPid <= MMIOSpace)
    {
	// Do MMIO mapping.
	uint64_t effAddrPage = (effAddr - FULL_MEM_SIZE) / PAGESIZE;

	// Check for valid entry in MMIO map.
	uint64_t mmioMapEntry = mmioMapT[effAddrPage];
	if (0 == mmioMapEntry)
	{
	    lock.unlock();
	    return false;
	}
	
	uint64_t mmioMapPage = mmioMapEntry / PAGESIZE;

	// Update PTE.
	volatile pte_t& pte = getPte(effAddrPage, 1);
	if ((getTid(pte) == effPid) && 
	    (getPage(pte) == mmioMapPage) &&
	    (isValid(pte)))
	{
	    // Already present, maybe another thread.
	    lock.unlock();
	    return true;
	}
	if (isValid(pte))	// Invalidate if already valid.
	    setValid(false, pte);
	defaultPte(pte);
	setTid(effPid, pte);
	setPage(mmioMapPage, pte);
	setAccess(CI_ACCESS, pte);
	setValid(true, pte);
	
	lock.unlock();
	return true;
    }
    else
    {
	// TODO: Do dynamic stack mapping.
	lock.unlock();
	return false;
    }
}

void* VmmManager::_mmioMap(void* ra, size_t pages)
{
    lock.lock();

    ssize_t match = -1;
    uint64_t _ra = (uint64_t) ra;

    // Search for memory already mapped in.
    for (int i = 0; i < MMIO_T_ENTRIES; i++)
    {
	if ((mmioMapT[i] & ~(PAGESIZE - 1) == _ra))
	{
	    if (i + pages < MMIO_T_ENTRIES)
	    {
		bool matched = true;
		for (int j = 1; j < pages; j++)
		{
		    if ((mmioMapT[i+j] & ~(PAGESIZE - 1) != 
			(_ra + (j*PAGESIZE))))
		    {
			matched = false; 
			break;
		    }
		}
		if (matched)
		{
		    match = i;
		    break;
		}
	    }
	}
    }
    
    // Found region already mapped in.
    if (-1 != match)
    {
	// Increment ref counts.
	for (int i = 0; i < pages; i++)
	{
	    mmioMapT[match + i]++;
	}
	// Return calculated effective address.
	lock.unlock();
	return (void*)(FULL_MEM_SIZE + (match * PAGESIZE));
    }

    // Search for empty region in map.
    for (int i = 0; i < MMIO_T_ENTRIES; i++)
    {
	if (0 == mmioMapT[i])
	{
	    bool matched = true;
	    for (int j = 1; j < pages; j++)
	    {
		if (0 != mmioMapT[i+j])
		{
		    matched = false;
		    break;
		}
	    }
	    if (matched)
	    {
		match = i;
		break;
	    }
	}
    }

    // Found region to use for map.
    if (-1 != match)
    {
	for (int i = 0; i < pages; i++)
	{
	    mmioMapT[match + i] = _ra + 1; // RA + ref count of 1.
	}

	lock.unlock();
	return (void*)(FULL_MEM_SIZE + (match * PAGESIZE));
    }

    // No entry found and no space for more, return NULL.
    lock.unlock();
    return NULL;
}

int VmmManager::_mmioUnmap(void* ea, size_t pages)
{
    return -1;
}

OpenPOWER on IntegriCloud