xref: /openbmc/linux/arch/um/kernel/mem.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
10d1fb0a4SAlex Dewar // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
3009ec2a9SJeff Dike  * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
41da177e4SLinus Torvalds  */
51da177e4SLinus Torvalds 
6009ec2a9SJeff Dike #include <linux/stddef.h>
773395a00SAl Viro #include <linux/module.h>
82013288fSMike Rapoport #include <linux/memblock.h>
9009ec2a9SJeff Dike #include <linux/highmem.h>
10009ec2a9SJeff Dike #include <linux/mm.h>
11009ec2a9SJeff Dike #include <linux/swap.h>
125a0e3ad6STejun Heo #include <linux/slab.h>
13009ec2a9SJeff Dike #include <asm/fixmap.h>
14009ec2a9SJeff Dike #include <asm/page.h>
1537185b33SAl Viro #include <as-layout.h>
1637185b33SAl Viro #include <init.h>
1737185b33SAl Viro #include <kern.h>
1837185b33SAl Viro #include <kern_util.h>
1937185b33SAl Viro #include <mem_user.h>
2037185b33SAl Viro #include <os.h>
215b301409SPatricia Alfonso #include <linux/sched/task.h>
225b301409SPatricia Alfonso 
235b301409SPatricia Alfonso #ifdef CONFIG_KASAN
245b301409SPatricia Alfonso int kasan_um_is_ready;
kasan_init(void)255b301409SPatricia Alfonso void kasan_init(void)
265b301409SPatricia Alfonso {
275b301409SPatricia Alfonso 	/*
285b301409SPatricia Alfonso 	 * kasan_map_memory will map all of the required address space and
295b301409SPatricia Alfonso 	 * the host machine will allocate physical memory as necessary.
305b301409SPatricia Alfonso 	 */
315b301409SPatricia Alfonso 	kasan_map_memory((void *)KASAN_SHADOW_START, KASAN_SHADOW_SIZE);
325b301409SPatricia Alfonso 	init_task.kasan_depth = 0;
335b301409SPatricia Alfonso 	kasan_um_is_ready = true;
345b301409SPatricia Alfonso }
355b301409SPatricia Alfonso 
365b301409SPatricia Alfonso static void (*kasan_init_ptr)(void)
375b301409SPatricia Alfonso __section(".kasan_init") __used
385b301409SPatricia Alfonso = kasan_init;
395b301409SPatricia Alfonso #endif
401da177e4SLinus Torvalds 
416bf79482SJeff Dike /* allocated in paging_init, zeroed in mem_init, and unchanged thereafter */
421da177e4SLinus Torvalds unsigned long *empty_zero_page = NULL;
4373395a00SAl Viro EXPORT_SYMBOL(empty_zero_page);
4480e39311SJeff Dike 
4580e39311SJeff Dike /*
4680e39311SJeff Dike  * Initialized during boot, and readonly for initializing page tables
4780e39311SJeff Dike  * afterwards
4880e39311SJeff Dike  */
491da177e4SLinus Torvalds pgd_t swapper_pg_dir[PTRS_PER_PGD];
5080e39311SJeff Dike 
5180e39311SJeff Dike /* Initialized at boot time, and readonly after that */
529902abd7SJeff Dike unsigned long long highmem;
535d38f324SErel Geron EXPORT_SYMBOL(highmem);
541da177e4SLinus Torvalds int kmalloc_ok = 0;
551da177e4SLinus Torvalds 
5680e39311SJeff Dike /* Used during early boot */
571da177e4SLinus Torvalds static unsigned long brk_end;
581da177e4SLinus Torvalds 
mem_init(void)5997a1fcbbSJeff Dike void __init mem_init(void)
601da177e4SLinus Torvalds {
611da177e4SLinus Torvalds 	/* clear the zero-page */
62c0a9290eSWANG Cong 	memset(empty_zero_page, 0, PAGE_SIZE);
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds 	/* Map in the area just after the brk now that kmalloc is about
651da177e4SLinus Torvalds 	 * to be turned on.
661da177e4SLinus Torvalds 	 */
671da177e4SLinus Torvalds 	brk_end = (unsigned long) UML_ROUND_UP(sbrk(0));
68ab26a527SJeff Dike 	map_memory(brk_end, __pa(brk_end), uml_reserved - brk_end, 1, 1, 0);
694421cca0SMike Rapoport 	memblock_free((void *)brk_end, uml_reserved - brk_end);
701da177e4SLinus Torvalds 	uml_reserved = brk_end;
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds 	/* this will put all low memory onto the freelists */
73c6ffc5caSMike Rapoport 	memblock_free_all();
74ca79b0c2SArun KS 	max_low_pfn = totalram_pages();
753d6357deSArun KS 	max_pfn = max_low_pfn;
761da177e4SLinus Torvalds 	kmalloc_ok = 1;
771da177e4SLinus Torvalds }
781da177e4SLinus Torvalds 
7912f49643SJeff Dike /*
8012f49643SJeff Dike  * Create a page table and place a pointer to it in a middle page
8112f49643SJeff Dike  * directory entry.
8212f49643SJeff Dike  */
one_page_table_init(pmd_t * pmd)8312f49643SJeff Dike static void __init one_page_table_init(pmd_t *pmd)
8412f49643SJeff Dike {
8512f49643SJeff Dike 	if (pmd_none(*pmd)) {
86e8625dceSMike Rapoport 		pte_t *pte = (pte_t *) memblock_alloc_low(PAGE_SIZE,
87e8625dceSMike Rapoport 							  PAGE_SIZE);
888a7f97b9SMike Rapoport 		if (!pte)
898a7f97b9SMike Rapoport 			panic("%s: Failed to allocate %lu bytes align=%lx\n",
908a7f97b9SMike Rapoport 			      __func__, PAGE_SIZE, PAGE_SIZE);
918a7f97b9SMike Rapoport 
9212f49643SJeff Dike 		set_pmd(pmd, __pmd(_KERNPG_TABLE +
9312f49643SJeff Dike 					   (unsigned long) __pa(pte)));
9424271ffeSYang Li 		BUG_ON(pte != pte_offset_kernel(pmd, 0));
9512f49643SJeff Dike 	}
9612f49643SJeff Dike }
9712f49643SJeff Dike 
one_md_table_init(pud_t * pud)9812f49643SJeff Dike static void __init one_md_table_init(pud_t *pud)
9912f49643SJeff Dike {
10012f49643SJeff Dike #ifdef CONFIG_3_LEVEL_PGTABLES
101e8625dceSMike Rapoport 	pmd_t *pmd_table = (pmd_t *) memblock_alloc_low(PAGE_SIZE, PAGE_SIZE);
1028a7f97b9SMike Rapoport 	if (!pmd_table)
1038a7f97b9SMike Rapoport 		panic("%s: Failed to allocate %lu bytes align=%lx\n",
1048a7f97b9SMike Rapoport 		      __func__, PAGE_SIZE, PAGE_SIZE);
1058a7f97b9SMike Rapoport 
10612f49643SJeff Dike 	set_pud(pud, __pud(_KERNPG_TABLE + (unsigned long) __pa(pmd_table)));
1078bd18ef9SChangcheng Deng 	BUG_ON(pmd_table != pmd_offset(pud, 0));
10812f49643SJeff Dike #endif
10912f49643SJeff Dike }
11012f49643SJeff Dike 
fixrange_init(unsigned long start,unsigned long end,pgd_t * pgd_base)1111da177e4SLinus Torvalds static void __init fixrange_init(unsigned long start, unsigned long end,
1121da177e4SLinus Torvalds 				 pgd_t *pgd_base)
1131da177e4SLinus Torvalds {
1141da177e4SLinus Torvalds 	pgd_t *pgd;
115e19f97edSMike Rapoport 	p4d_t *p4d;
11612f49643SJeff Dike 	pud_t *pud;
1171da177e4SLinus Torvalds 	pmd_t *pmd;
1181da177e4SLinus Torvalds 	int i, j;
1191da177e4SLinus Torvalds 	unsigned long vaddr;
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds 	vaddr = start;
1221da177e4SLinus Torvalds 	i = pgd_index(vaddr);
1231da177e4SLinus Torvalds 	j = pmd_index(vaddr);
1241da177e4SLinus Torvalds 	pgd = pgd_base + i;
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds 	for ( ; (i < PTRS_PER_PGD) && (vaddr < end); pgd++, i++) {
127e19f97edSMike Rapoport 		p4d = p4d_offset(pgd, vaddr);
128e19f97edSMike Rapoport 		pud = pud_offset(p4d, vaddr);
12912f49643SJeff Dike 		if (pud_none(*pud))
13012f49643SJeff Dike 			one_md_table_init(pud);
13112f49643SJeff Dike 		pmd = pmd_offset(pud, vaddr);
132655e4ed0SJeff Dike 		for (; (j < PTRS_PER_PMD) && (vaddr < end); pmd++, j++) {
13312f49643SJeff Dike 			one_page_table_init(pmd);
1341da177e4SLinus Torvalds 			vaddr += PMD_SIZE;
1351da177e4SLinus Torvalds 		}
1361da177e4SLinus Torvalds 		j = 0;
1371da177e4SLinus Torvalds 	}
1381da177e4SLinus Torvalds }
1391da177e4SLinus Torvalds 
fixaddr_user_init(void)1401da177e4SLinus Torvalds static void __init fixaddr_user_init( void)
1411da177e4SLinus Torvalds {
1429a0b3869Sviro@ZenIV.linux.org.uk #ifdef CONFIG_ARCH_REUSE_HOST_VSYSCALL_AREA
1431da177e4SLinus Torvalds 	long size = FIXADDR_USER_END - FIXADDR_USER_START;
1441da177e4SLinus Torvalds 	pte_t *pte;
145655e4ed0SJeff Dike 	phys_t p;
146655e4ed0SJeff Dike 	unsigned long v, vaddr = FIXADDR_USER_START;
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds 	if (!size)
1491da177e4SLinus Torvalds 		return;
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds 	fixrange_init( FIXADDR_USER_START, FIXADDR_USER_END, swapper_pg_dir);
152e8625dceSMike Rapoport 	v = (unsigned long) memblock_alloc_low(size, PAGE_SIZE);
1538a7f97b9SMike Rapoport 	if (!v)
1548a7f97b9SMike Rapoport 		panic("%s: Failed to allocate %lu bytes align=%lx\n",
1558a7f97b9SMike Rapoport 		      __func__, size, PAGE_SIZE);
1568a7f97b9SMike Rapoport 
157655e4ed0SJeff Dike 	memcpy((void *) v , (void *) FIXADDR_USER_START, size);
158655e4ed0SJeff Dike 	p = __pa(v);
159009ec2a9SJeff Dike 	for ( ; size > 0; size -= PAGE_SIZE, vaddr += PAGE_SIZE,
160655e4ed0SJeff Dike 		      p += PAGE_SIZE) {
161e05c7b1fSMike Rapoport 		pte = virt_to_kpte(vaddr);
162655e4ed0SJeff Dike 		pte_set_val(*pte, p, PAGE_READONLY);
1631da177e4SLinus Torvalds 	}
1641da177e4SLinus Torvalds #endif
1651da177e4SLinus Torvalds }
1661da177e4SLinus Torvalds 
paging_init(void)16736e45463SJeff Dike void __init paging_init(void)
1681da177e4SLinus Torvalds {
169fa3354e4SMike Rapoport 	unsigned long max_zone_pfn[MAX_NR_ZONES] = { 0 };
170fa3354e4SMike Rapoport 	unsigned long vaddr;
1711da177e4SLinus Torvalds 
172e8625dceSMike Rapoport 	empty_zero_page = (unsigned long *) memblock_alloc_low(PAGE_SIZE,
173e8625dceSMike Rapoport 							       PAGE_SIZE);
1748a7f97b9SMike Rapoport 	if (!empty_zero_page)
1758a7f97b9SMike Rapoport 		panic("%s: Failed to allocate %lu bytes align=%lx\n",
1768a7f97b9SMike Rapoport 		      __func__, PAGE_SIZE, PAGE_SIZE);
1778a7f97b9SMike Rapoport 
178fa3354e4SMike Rapoport 	max_zone_pfn[ZONE_NORMAL] = end_iomem >> PAGE_SHIFT;
179fa3354e4SMike Rapoport 	free_area_init(max_zone_pfn);
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds 	/*
1821da177e4SLinus Torvalds 	 * Fixed mappings, only the page table structure has to be
1831da177e4SLinus Torvalds 	 * created - mappings will be set by set_fixmap():
1841da177e4SLinus Torvalds 	 */
1851da177e4SLinus Torvalds 	vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
1861da177e4SLinus Torvalds 	fixrange_init(vaddr, FIXADDR_TOP, swapper_pg_dir);
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds 	fixaddr_user_init();
1891da177e4SLinus Torvalds }
1901da177e4SLinus Torvalds 
1918192ab42SJeff Dike /*
1928192ab42SJeff Dike  * This can't do anything because nothing in the kernel image can be freed
1931da177e4SLinus Torvalds  * since it's not in kernel physical memory.
1941da177e4SLinus Torvalds  */
1951da177e4SLinus Torvalds 
free_initmem(void)1961da177e4SLinus Torvalds void free_initmem(void)
1971da177e4SLinus Torvalds {
1981da177e4SLinus Torvalds }
1991da177e4SLinus Torvalds 
2008192ab42SJeff Dike /* Allocate and free page tables. */
2011da177e4SLinus Torvalds 
pgd_alloc(struct mm_struct * mm)2021da177e4SLinus Torvalds pgd_t *pgd_alloc(struct mm_struct *mm)
2031da177e4SLinus Torvalds {
2041da177e4SLinus Torvalds 	pgd_t *pgd = (pgd_t *)__get_free_page(GFP_KERNEL);
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 	if (pgd) {
2071da177e4SLinus Torvalds 		memset(pgd, 0, USER_PTRS_PER_PGD * sizeof(pgd_t));
2081da177e4SLinus Torvalds 		memcpy(pgd + USER_PTRS_PER_PGD,
2091da177e4SLinus Torvalds 		       swapper_pg_dir + USER_PTRS_PER_PGD,
2101da177e4SLinus Torvalds 		       (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t));
2111da177e4SLinus Torvalds 	}
2121da177e4SLinus Torvalds 	return pgd;
2131da177e4SLinus Torvalds }
2141da177e4SLinus Torvalds 
uml_kmalloc(int size,int flags)21543f5b308SJeff Dike void *uml_kmalloc(int size, int flags)
21643f5b308SJeff Dike {
21743f5b308SJeff Dike 	return kmalloc(size, flags);
21843f5b308SJeff Dike }
219*91a8da02SAnshuman Khandual 
220*91a8da02SAnshuman Khandual static const pgprot_t protection_map[16] = {
221*91a8da02SAnshuman Khandual 	[VM_NONE]					= PAGE_NONE,
222*91a8da02SAnshuman Khandual 	[VM_READ]					= PAGE_READONLY,
223*91a8da02SAnshuman Khandual 	[VM_WRITE]					= PAGE_COPY,
224*91a8da02SAnshuman Khandual 	[VM_WRITE | VM_READ]				= PAGE_COPY,
225*91a8da02SAnshuman Khandual 	[VM_EXEC]					= PAGE_READONLY,
226*91a8da02SAnshuman Khandual 	[VM_EXEC | VM_READ]				= PAGE_READONLY,
227*91a8da02SAnshuman Khandual 	[VM_EXEC | VM_WRITE]				= PAGE_COPY,
228*91a8da02SAnshuman Khandual 	[VM_EXEC | VM_WRITE | VM_READ]			= PAGE_COPY,
229*91a8da02SAnshuman Khandual 	[VM_SHARED]					= PAGE_NONE,
230*91a8da02SAnshuman Khandual 	[VM_SHARED | VM_READ]				= PAGE_READONLY,
231*91a8da02SAnshuman Khandual 	[VM_SHARED | VM_WRITE]				= PAGE_SHARED,
232*91a8da02SAnshuman Khandual 	[VM_SHARED | VM_WRITE | VM_READ]		= PAGE_SHARED,
233*91a8da02SAnshuman Khandual 	[VM_SHARED | VM_EXEC]				= PAGE_READONLY,
234*91a8da02SAnshuman Khandual 	[VM_SHARED | VM_EXEC | VM_READ]			= PAGE_READONLY,
235*91a8da02SAnshuman Khandual 	[VM_SHARED | VM_EXEC | VM_WRITE]		= PAGE_SHARED,
236*91a8da02SAnshuman Khandual 	[VM_SHARED | VM_EXEC | VM_WRITE | VM_READ]	= PAGE_SHARED
237*91a8da02SAnshuman Khandual };
238*91a8da02SAnshuman Khandual DECLARE_VM_GET_PAGE_PROT
239