Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
/*	$NetBSD: loadfile_machdep.c,v 1.17 2022/04/29 20:24:02 rin Exp $	*/

/*-
 * Copyright (c) 2005 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This work is based on the code contributed by Robert Drehmel to the
 * FreeBSD project.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/param.h>

#include <lib/libsa/stand.h>
#include <lib/libkern/libkern.h>

#include <machine/pte.h>
#include <machine/cpu.h>
#include <machine/ctlreg.h>
#include <machine/vmparam.h>
#include <machine/promlib.h>
#include <machine/hypervisor.h>

#include "boot.h"
#include "openfirm.h"


#define MAXSEGNUM	50
#define hi(val)		((uint32_t)(((val) >> 32) & (uint32_t)-1))
#define lo(val)		((uint32_t)((val) & (uint32_t)-1))


typedef int phandle_t;

extern void	itlb_enter(vaddr_t, uint32_t, uint32_t);
extern void	dtlb_enter(vaddr_t, uint32_t, uint32_t);
extern void	dtlb_replace(vaddr_t, uint32_t, uint32_t);
extern vaddr_t	itlb_va_to_pa(vaddr_t);
extern vaddr_t	dtlb_va_to_pa(vaddr_t);

static void	tlb_init(void);
static void	tlb_init_sun4u(void);
#ifdef SUN4V
static void	tlb_init_sun4v(void);
#endif
void	sparc64_finalize_tlb_sun4u(u_long);
#ifdef SUN4V
void	sparc64_finalize_tlb_sun4v(u_long);
#endif
static int	mmu_mapin(vaddr_t, vsize_t);
static int	mmu_mapin_sun4u(vaddr_t, vsize_t);
#ifdef SUN4V
static int	mmu_mapin_sun4v(vaddr_t, vsize_t);
#endif
static ssize_t	mmu_read(int, void *, size_t);
static void*	mmu_memcpy(void *, const void *, size_t);
static void*	mmu_memset(void *, int, size_t);
static void	mmu_freeall(void);

static int	ofw_mapin(vaddr_t, vsize_t);
static ssize_t	ofw_read(int, void *, size_t);
static void*	ofw_memcpy(void *, const void *, size_t);
static void*	ofw_memset(void *, int, size_t);
static void	ofw_freeall(void);

#if 0
static int	nop_mapin(vaddr_t, vsize_t);
#endif
static ssize_t	nop_read(int, void *, size_t);
static void*	nop_memcpy(void *, const void *, size_t);
static void*	nop_memset(void *, int, size_t);
static void	nop_freeall(void);


struct tlb_entry *dtlb_store = 0;
struct tlb_entry *itlb_store = 0;

int dtlb_slot;
int itlb_slot;
int dtlb_slot_max;
int itlb_slot_max;

static struct kvamap {
	uint64_t start;
	uint64_t end;
} kvamap[MAXSEGNUM];

static struct memsw {
	ssize_t	(* read)(int f, void *addr, size_t size);
	void*	(* memcpy)(void *dst, const void *src, size_t size);
	void*	(* memset)(void *dst, int c, size_t size);
	void	(* freeall)(void);
} memswa[] = {
	{ nop_read, nop_memcpy, nop_memset, nop_freeall },
	{ ofw_read, ofw_memcpy, ofw_memset, ofw_freeall },
	{ mmu_read, mmu_memcpy, mmu_memset, mmu_freeall }
};

static struct memsw *memsw = &memswa[0];

#ifdef SUN4V
static int sun4v = 0;
#endif

/*
 * Check if a memory region is already mapped. Return length and virtual
 * address of unmapped sub-region, if any.
 */
static uint64_t
kvamap_extract(vaddr_t va, vsize_t len, vaddr_t *new_va)
{
	int i;

	*new_va  = va;
	for (i = 0; (len > 0) && (i < MAXSEGNUM); i++) {
		if (kvamap[i].start == 0)
			break;
		if ((kvamap[i].start <= va) && (va < kvamap[i].end)) {
			uint64_t va_len = kvamap[i].end - va;
			len = (va_len < len) ? len - va_len : 0;
			*new_va = kvamap[i].end;
		}
	}

	return len;
}

/*
 * Record new kernel mapping.
 */
static void
kvamap_enter(uint64_t va, uint64_t len)
{
	int i;

	DPRINTF(("kvamap_enter: %d@%p\n", (int)len, (void*)(u_long)va));
	for (i = 0; (len > 0) && (i < MAXSEGNUM); i++) {
		if (kvamap[i].start == 0) {
			kvamap[i].start = va;
			kvamap[i].end = va + len;
			break;
		}
	}

	if (i == MAXSEGNUM) {
		panic("Too many allocations requested.");
	}
}

/*
 * Initialize TLB as required by MMU mapping functions.
 */
static void
tlb_init(void)
{
	phandle_t root;
#ifdef SUN4V	
	char buf[128];
#endif	

	if (dtlb_store != NULL) {
		return;
	}

	if ( (root = prom_findroot()) == -1) {
		panic("tlb_init: prom_findroot()");
	}
#ifdef SUN4V		
	if (_prom_getprop(root, "compatible", buf, sizeof(buf)) > 0 &&
		    strcmp(buf, "sun4v") == 0) {
		tlb_init_sun4v();
		sun4v = 1;
	}
	else {
#endif
		tlb_init_sun4u();
#ifdef SUN4V		
	}
#endif

	dtlb_store = alloc(dtlb_slot_max * sizeof(*dtlb_store));
	itlb_store = alloc(itlb_slot_max * sizeof(*itlb_store));
	if (dtlb_store == NULL || itlb_store == NULL) {
		panic("tlb_init: malloc");
	}

	dtlb_slot = itlb_slot = 0;
}

/*
 * Initialize TLB as required by MMU mapping functions - sun4u.
 */
static void
tlb_init_sun4u(void)
{
	phandle_t child;
	phandle_t root;
	char buf[128];
	bool foundcpu = false;
	u_int bootcpu;
	u_int cpu;

	bootcpu = get_cpuid();

	if ( (root = prom_findroot()) == -1) {
		panic("tlb_init: prom_findroot()");
	}

	for (child = prom_firstchild(root); child != 0;
			child = prom_nextsibling(child)) {
		if (child == -1) {
			panic("tlb_init: OF_child");
		}
		if (_prom_getprop(child, "device_type", buf, sizeof(buf)) > 0 &&
		    strcmp(buf, "cpu") == 0) {
			if (_prom_getprop(child, "upa-portid", &cpu,
			    sizeof(cpu)) == -1 && _prom_getprop(child, "portid",
			    &cpu, sizeof(cpu)) == -1)
				panic("tlb_init: prom_getprop");
			foundcpu = true;
			if (cpu == bootcpu)
				break;
		}
	}
	if (!foundcpu)
		panic("tlb_init: no cpu found!");
	if (cpu != bootcpu)
		panic("tlb_init: no node for bootcpu?!?!");
	if (_prom_getprop(child, "#dtlb-entries", &dtlb_slot_max,
	    sizeof(dtlb_slot_max)) == -1 ||
	    _prom_getprop(child, "#itlb-entries", &itlb_slot_max,
	    sizeof(itlb_slot_max)) == -1)
		panic("tlb_init: prom_getprop");
}

#ifdef SUN4V
/*
 * Initialize TLB as required by MMU mapping functions - sun4v.
 */
static void
tlb_init_sun4v(void)
{
	psize_t len;
	paddr_t pa;
	int64_t hv_rc;

	hv_mach_desc((paddr_t)NULL, &len); /* Trick to get actual length */
	if ( !len ) {
		panic("init_tlb: hv_mach_desc() failed");
	}
	pa = OF_alloc_phys(len, 16);
	if ( pa == -1 ) {
		panic("OF_alloc_phys() failed");
	}
	hv_rc = hv_mach_desc(pa, &len);
	if (hv_rc != H_EOK) {
		panic("hv_mach_desc() failed");
	}
	/* XXX dig out TLB node info - 64 is ok for loading the kernel */
	dtlb_slot_max = itlb_slot_max = 64;
}
#endif

/*
 * Map requested memory region with permanent 4MB pages.
 */
static int
mmu_mapin(vaddr_t rva, vsize_t len)
{
	len  = roundup2(len + (rva & PAGE_MASK_4M), PAGE_SIZE_4M);
	rva &= ~PAGE_MASK_4M;

	tlb_init();

#if SUN4V	
	if ( sun4v )
		return mmu_mapin_sun4v(rva, len);
	else
#endif		
		return mmu_mapin_sun4u(rva, len);
}

/*
 * Map requested memory region with permanent 4MB pages - sun4u.
 */
static int
mmu_mapin_sun4u(vaddr_t rva, vsize_t len)
{
	uint64_t data;
	paddr_t pa;
	vaddr_t va, mva;

	for (pa = (paddr_t)-1; len > 0; rva = va) {
		if ( (len = kvamap_extract(rva, len, &va)) == 0) {
			/* The rest is already mapped */
			break;
		}

		if (dtlb_va_to_pa(va) == (u_long)-1 ||
		    itlb_va_to_pa(va) == (u_long)-1) {
			/* Allocate a physical page, claim the virtual area */
			if (pa == (paddr_t)-1) {
				pa = OF_alloc_phys(PAGE_SIZE_4M, PAGE_SIZE_4M);
				if (pa == (paddr_t)-1)
					panic("out of memory");
				mva = OF_claim_virt(va, PAGE_SIZE_4M);
				if (mva != va) {
					panic("can't claim virtual page "
					    "(wanted %#lx, got %#lx)",
					    va, mva);
				}
				/* The mappings may have changed, be paranoid. */
				continue;
			}

			/*
			 * Actually, we can only allocate two pages less at
			 * most (depending on the kernel TSB size).
			 */
			if (dtlb_slot >= dtlb_slot_max)
				panic("mmu_mapin: out of dtlb_slots");
			if (itlb_slot >= itlb_slot_max)
				panic("mmu_mapin: out of itlb_slots");

			DPRINTF(("mmu_mapin: 0x%lx:0x%x.0x%x\n", va,
			    hi(pa), lo(pa)));

			data = SUN4U_TSB_DATA(0,	/* global */
					PGSZ_4M,	/* 4mb page */
					pa,		/* phys.address */
					1,		/* privileged */
					1,		/* write */
					1,		/* cache */
					1,		/* alias */
					1,		/* valid */
					0,		/* endianness */
					0		/* wc */
					);
			data |= SUN4U_TLB_L | SUN4U_TLB_CV; /* locked, virt.cache */

			dtlb_store[dtlb_slot].te_pa = pa;
			dtlb_store[dtlb_slot].te_va = va;
			dtlb_slot++;
			dtlb_enter(va, hi(data), lo(data));
			pa = (paddr_t)-1;
		}

		kvamap_enter(va, PAGE_SIZE_4M);

		len -= len > PAGE_SIZE_4M ? PAGE_SIZE_4M : len;
		va += PAGE_SIZE_4M;
	}

	if (pa != (paddr_t)-1) {
		OF_free_phys(pa, PAGE_SIZE_4M);
	}

	return (0);
}

#ifdef SUN4V
/*
 * Map requested memory region with permanent 4MB pages - sun4v.
 */
static int
mmu_mapin_sun4v(vaddr_t rva, vsize_t len)
{
	uint64_t data;
	paddr_t pa;
	vaddr_t va, mva;
	int64_t hv_rc;

	for (pa = (paddr_t)-1; len > 0; rva = va) {
		if ( (len = kvamap_extract(rva, len, &va)) == 0) {
			/* The rest is already mapped */
			break;
		}

		/* Allocate a physical page, claim the virtual area */
		if (pa == (paddr_t)-1) {
			pa = OF_alloc_phys(PAGE_SIZE_4M, PAGE_SIZE_4M);
			if (pa == (paddr_t)-1)
				panic("out of memory");
			mva = OF_claim_virt(va, PAGE_SIZE_4M);
			if (mva != va) {
				panic("can't claim virtual page "
				    "(wanted %#lx, got %#lx)",
				    va, mva);
			}
		}

		/*
		 * Actually, we can only allocate two pages less at
		 * most (depending on the kernel TSB size).
		 */
		if (dtlb_slot >= dtlb_slot_max)
			panic("mmu_mapin: out of dtlb_slots");
		if (itlb_slot >= itlb_slot_max)
			panic("mmu_mapin: out of itlb_slots");
		
		DPRINTF(("mmu_mapin: 0x%lx:0x%x.0x%x\n", va,
		    hi(pa), lo(pa)));

		data = SUN4V_TSB_DATA(
			0,		/* global */
			PGSZ_4M,	/* 4mb page */
			pa,		/* phys.address */
			1,		/* privileged */
			1,		/* write */
			1,		/* cache */
			1,		/* alias */
			1,		/* valid */
			0,		/* endianness */
			0		/* wc */
			);
		data |= SUN4V_TLB_CV; /* virt.cache */
		
		dtlb_store[dtlb_slot].te_pa = pa;
		dtlb_store[dtlb_slot].te_va = va;
		dtlb_slot++;
		hv_rc = hv_mmu_map_perm_addr(va, data, MAP_DTLB);
		if ( hv_rc != H_EOK ) {
			panic("hv_mmu_map_perm_addr() failed - rc = %ld", hv_rc);
		}

		kvamap_enter(va, PAGE_SIZE_4M);

		pa = (paddr_t)-1;

		len -= len > PAGE_SIZE_4M ? PAGE_SIZE_4M : len;
		va += PAGE_SIZE_4M;
	}

	if (pa != (paddr_t)-1) {
		OF_free_phys(pa, PAGE_SIZE_4M);
	}

	return (0);
}
#endif

static ssize_t
mmu_read(int f, void *addr, size_t size)
{
	mmu_mapin((vaddr_t)addr, size);
	return read(f, addr, size);
}

static void*
mmu_memcpy(void *dst, const void *src, size_t size)
{
	mmu_mapin((vaddr_t)dst, size);
	return memcpy(dst, src, size);
}

static void*
mmu_memset(void *dst, int c, size_t size)
{
	mmu_mapin((vaddr_t)dst, size);
	return memset(dst, c, size);
}

static void
mmu_freeall(void)
{
	int i;

	dtlb_slot = itlb_slot = 0;
	for (i = 0; i < MAXSEGNUM; i++) {
		/* XXX return all mappings to PROM and unmap the pages! */
		kvamap[i].start = kvamap[i].end = 0;
	}
}

/*
 * Claim requested memory region in OpenFirmware allocation pool.
 */
static int
ofw_mapin(vaddr_t rva, vsize_t len)
{
	vaddr_t va;

	len  = roundup2(len + (rva & PAGE_MASK_4M), PAGE_SIZE_4M);
	rva &= ~PAGE_MASK_4M;

	if ( (len = kvamap_extract(rva, len, &va)) != 0) {
		if (OF_claim((void *)(long)va, len, PAGE_SIZE_4M) == (void*)-1){
			panic("ofw_mapin: Cannot claim memory.");
		}
		kvamap_enter(va, len);
	}

	return (0);
}

static ssize_t
ofw_read(int f, void *addr, size_t size)
{
	ofw_mapin((vaddr_t)addr, size);
	return read(f, addr, size);
}

static void*
ofw_memcpy(void *dst, const void *src, size_t size)
{
	ofw_mapin((vaddr_t)dst, size);
	return memcpy(dst, src, size);
}

static void*
ofw_memset(void *dst, int c, size_t size)
{
	ofw_mapin((vaddr_t)dst, size);
	return memset(dst, c, size);
}

static void
ofw_freeall(void)
{
	int i;

	dtlb_slot = itlb_slot = 0;
	for (i = 0; i < MAXSEGNUM; i++) {
		OF_release((void*)(u_long)kvamap[i].start,
				(u_int)(kvamap[i].end - kvamap[i].start));
		kvamap[i].start = kvamap[i].end = 0;
	}
}

/*
 * NOP implementation exists solely for kernel header loading sake. Here
 * we use alloc() interface to allocate memory and avoid doing some dangerous
 * things.
 */
static ssize_t
nop_read(int f, void *addr, size_t size)
{
	return read(f, addr, size);
}

static void*
nop_memcpy(void *dst, const void *src, size_t size)
{
	/*
	 * Real NOP to make LOAD_HDR work: loadfile_elfXX copies ELF headers
	 * right after the highest kernel address which will not be mapped with
	 * nop_XXX operations.
	 */
	return (dst);
}

static void*
nop_memset(void *dst, int c, size_t size)
{
	return memset(dst, c, size);
}

static void
nop_freeall(void)
{ }

/*
 * loadfile() hooks.
 */
ssize_t
sparc64_read(int f, void *addr, size_t size)
{
	return (*memsw->read)(f, addr, size);
}

void*
sparc64_memcpy(void *dst, const void *src, size_t size)
{
	return (*memsw->memcpy)(dst, src, size);
}

void*
sparc64_memset(void *dst, int c, size_t size)
{
	return (*memsw->memset)(dst, c, size);
}

/*
 * Remove write permissions from text mappings in the dTLB.
 * Add entries in the iTLB.
 */
void
sparc64_finalize_tlb(u_long data_va)
{
#ifdef SUN4V
	if ( sun4v )
		sparc64_finalize_tlb_sun4v(data_va);
	else
#endif	
		sparc64_finalize_tlb_sun4u(data_va);
}

/*
 * Remove write permissions from text mappings in the dTLB - sun4u.
 * Add entries in the iTLB.
 */
void
sparc64_finalize_tlb_sun4u(u_long data_va)
{
	int i;
	int64_t data;
	bool writable_text = false;

	for (i = 0; i < dtlb_slot; i++) {
		if (dtlb_store[i].te_va >= data_va) {
			/*
			 * If (for whatever reason) the start of the
			 * writable section is right at the start of
			 * the kernel, we need to map it into the ITLB
			 * nevertheless (and don't make it readonly).
			 */
			if (i == 0 && dtlb_store[i].te_va == data_va)
				writable_text = true;
			else
				continue;
		}

		data = SUN4U_TSB_DATA(0,	/* global */
				PGSZ_4M,	/* 4mb page */
				dtlb_store[i].te_pa,	/* phys.address */
				1,		/* privileged */
				0,		/* write */
				1,		/* cache */
				1,		/* alias */
				1,		/* valid */
				0,		/* endianness */
				0		/* wc */
				);
		data |= SUN4U_TLB_L | SUN4U_TLB_CV; /* locked, virt.cache */
		if (!writable_text)
			dtlb_replace(dtlb_store[i].te_va, hi(data), lo(data));
		itlb_store[itlb_slot] = dtlb_store[i];
		itlb_slot++;
		itlb_enter(dtlb_store[i].te_va, hi(data), lo(data));
	}
	if (writable_text)
		printf("WARNING: kernel text mapped writable!\n");

}

#ifdef SUN4V
/*
 * Remove write permissions from text mappings in the dTLB - sun4v.
 * Add entries in the iTLB.
 */
void
sparc64_finalize_tlb_sun4v(u_long data_va)
{
	int i;
	int64_t data;
	bool writable_text = false;
	int64_t hv_rc;

	for (i = 0; i < dtlb_slot; i++) {
		if (dtlb_store[i].te_va >= data_va) {
			/*
			 * If (for whatever reason) the start of the
			 * writable section is right at the start of
			 * the kernel, we need to map it into the ITLB
			 * nevertheless (and don't make it readonly).
			 */
			if (i == 0 && dtlb_store[i].te_va == data_va)
				writable_text = true;
			else
				continue;
		}

		data = SUN4V_TSB_DATA(
			0,		/* global */
			PGSZ_4M,	/* 4mb page */
			dtlb_store[i].te_pa,	/* phys.address */
			1,		/* privileged */
			0,		/* write */
			1,		/* cache */
			1,		/* alias */
			1,		/* valid */
			0,		/* endianness */
			0		/* wc */
			);
		data |= SUN4V_TLB_CV|SUN4V_TLB_X; /* virt.cache, executable */
		if (!writable_text) {
			hv_rc = hv_mmu_unmap_perm_addr(dtlb_store[i].te_va,
			                               MAP_DTLB);
			if ( hv_rc != H_EOK ) {
				panic("hv_mmu_unmap_perm_addr() failed - "
				      "rc = %ld", hv_rc);
			}
			hv_rc = hv_mmu_map_perm_addr(dtlb_store[i].te_va, data,
			                             MAP_DTLB);
			if ( hv_rc != H_EOK ) {
				panic("hv_mmu_map_perm_addr() failed - "
				      "rc = %ld", hv_rc);
			}
		}
		
		itlb_store[itlb_slot] = dtlb_store[i];
		itlb_slot++;
		hv_rc = hv_mmu_map_perm_addr(dtlb_store[i].te_va, data,
		                             MAP_ITLB);
		if ( hv_rc != H_EOK ) {
			panic("hv_mmu_map_perm_addr() failed - rc = %ld", hv_rc);
		}
	}
	if (writable_text)
		printf("WARNING: kernel text mapped writable!\n");
}
#endif

/*
 * Record kernel mappings in bootinfo structure.
 */
void
sparc64_bi_add(void)
{
	int i;
	int itlb_size, dtlb_size;
	struct btinfo_count bi_count;
	struct btinfo_tlb *bi_itlb, *bi_dtlb;

	bi_count.count = itlb_slot;
	bi_add(&bi_count, BTINFO_ITLB_SLOTS, sizeof(bi_count));
	bi_count.count = dtlb_slot;
	bi_add(&bi_count, BTINFO_DTLB_SLOTS, sizeof(bi_count));

	itlb_size = sizeof(*bi_itlb) + sizeof(struct tlb_entry) * itlb_slot;
	dtlb_size = sizeof(*bi_dtlb) + sizeof(struct tlb_entry) * dtlb_slot;

	bi_itlb = alloc(itlb_size);
	bi_dtlb = alloc(dtlb_size);

	if ((bi_itlb == NULL) || (bi_dtlb == NULL)) {
		panic("Out of memory in sparc64_bi_add.\n");
	}

	for (i = 0; i < itlb_slot; i++) {
		bi_itlb->tlb[i].te_va = itlb_store[i].te_va;
		bi_itlb->tlb[i].te_pa = itlb_store[i].te_pa;
	}
	bi_add(bi_itlb, BTINFO_ITLB, itlb_size);

	for (i = 0; i < dtlb_slot; i++) {
		bi_dtlb->tlb[i].te_va = dtlb_store[i].te_va;
		bi_dtlb->tlb[i].te_pa = dtlb_store[i].te_pa;
	}
	bi_add(bi_dtlb, BTINFO_DTLB, dtlb_size);
}

/*
 * Choose kernel image mapping strategy:
 *
 * LOADFILE_NOP_ALLOCATOR	To load kernel image headers
 * LOADFILE_OFW_ALLOCATOR	To map the kernel by OpenFirmware means
 * LOADFILE_MMU_ALLOCATOR	To use permanent 4MB mappings
 */
void
loadfile_set_allocator(int type)
{
	if (type >= (sizeof(memswa) / sizeof(struct memsw))) {
		panic("Bad allocator request.\n");
	}

	/*
	 * Release all memory claimed by previous allocator and schedule
	 * another allocator for succeeding memory allocation calls.
	 */
	(*memsw->freeall)();
	memsw = &memswa[type];
}