18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright (C) 2013 Altera Corporation
38c2ecf20Sopenharmony_ci * Copyright (C) 2010 Tobias Klauser <tklauser@distanz.ch>
48c2ecf20Sopenharmony_ci * Copyright (C) 2009 Wind River Systems Inc
58c2ecf20Sopenharmony_ci *   Implemented by fredrik.markstrom@gmail.com and ivarholmqvist@gmail.com
68c2ecf20Sopenharmony_ci * Copyright (C) 2004 Microtronix Datacom Ltd
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * based on arch/m68k/mm/init.c
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * This file is subject to the terms and conditions of the GNU General Public
118c2ecf20Sopenharmony_ci * License. See the file "COPYING" in the main directory of this archive
128c2ecf20Sopenharmony_ci * for more details.
138c2ecf20Sopenharmony_ci */
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <linux/signal.h>
168c2ecf20Sopenharmony_ci#include <linux/sched.h>
178c2ecf20Sopenharmony_ci#include <linux/kernel.h>
188c2ecf20Sopenharmony_ci#include <linux/errno.h>
198c2ecf20Sopenharmony_ci#include <linux/string.h>
208c2ecf20Sopenharmony_ci#include <linux/types.h>
218c2ecf20Sopenharmony_ci#include <linux/ptrace.h>
228c2ecf20Sopenharmony_ci#include <linux/mman.h>
238c2ecf20Sopenharmony_ci#include <linux/mm.h>
248c2ecf20Sopenharmony_ci#include <linux/init.h>
258c2ecf20Sopenharmony_ci#include <linux/pagemap.h>
268c2ecf20Sopenharmony_ci#include <linux/memblock.h>
278c2ecf20Sopenharmony_ci#include <linux/slab.h>
288c2ecf20Sopenharmony_ci#include <linux/binfmts.h>
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#include <asm/setup.h>
318c2ecf20Sopenharmony_ci#include <asm/page.h>
328c2ecf20Sopenharmony_ci#include <asm/sections.h>
338c2ecf20Sopenharmony_ci#include <asm/tlb.h>
348c2ecf20Sopenharmony_ci#include <asm/mmu_context.h>
358c2ecf20Sopenharmony_ci#include <asm/cpuinfo.h>
368c2ecf20Sopenharmony_ci#include <asm/processor.h>
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cipgd_t *pgd_current;
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci/*
418c2ecf20Sopenharmony_ci * paging_init() continues the virtual memory environment setup which
428c2ecf20Sopenharmony_ci * was begun by the code in arch/head.S.
438c2ecf20Sopenharmony_ci * The parameters are pointers to where to stick the starting and ending
448c2ecf20Sopenharmony_ci * addresses of available kernel virtual memory.
458c2ecf20Sopenharmony_ci */
468c2ecf20Sopenharmony_civoid __init paging_init(void)
478c2ecf20Sopenharmony_ci{
488c2ecf20Sopenharmony_ci	unsigned long max_zone_pfn[MAX_NR_ZONES] = { 0 };
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	pagetable_init();
518c2ecf20Sopenharmony_ci	pgd_current = swapper_pg_dir;
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	max_zone_pfn[ZONE_NORMAL] = max_mapnr;
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	/* pass the memory from the bootmem allocator to the main allocator */
568c2ecf20Sopenharmony_ci	free_area_init(max_zone_pfn);
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	flush_dcache_range((unsigned long)empty_zero_page,
598c2ecf20Sopenharmony_ci			(unsigned long)empty_zero_page + PAGE_SIZE);
608c2ecf20Sopenharmony_ci}
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_civoid __init mem_init(void)
638c2ecf20Sopenharmony_ci{
648c2ecf20Sopenharmony_ci	unsigned long end_mem   = memory_end; /* this must not include
658c2ecf20Sopenharmony_ci						kernel stack at top */
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	pr_debug("mem_init: start=%lx, end=%lx\n", memory_start, memory_end);
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	end_mem &= PAGE_MASK;
708c2ecf20Sopenharmony_ci	high_memory = __va(end_mem);
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	/* this will put all memory onto the freelists */
738c2ecf20Sopenharmony_ci	memblock_free_all();
748c2ecf20Sopenharmony_ci	mem_init_print_info(NULL);
758c2ecf20Sopenharmony_ci}
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_civoid __init mmu_init(void)
788c2ecf20Sopenharmony_ci{
798c2ecf20Sopenharmony_ci	flush_tlb_all();
808c2ecf20Sopenharmony_ci}
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci#define __page_aligned(order) __aligned(PAGE_SIZE << (order))
838c2ecf20Sopenharmony_cipgd_t swapper_pg_dir[PTRS_PER_PGD] __page_aligned(PGD_ORDER);
848c2ecf20Sopenharmony_cipte_t invalid_pte_table[PTRS_PER_PTE] __page_aligned(PTE_ORDER);
858c2ecf20Sopenharmony_cistatic struct page *kuser_page[1];
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_cistatic int alloc_kuser_page(void)
888c2ecf20Sopenharmony_ci{
898c2ecf20Sopenharmony_ci	extern char __kuser_helper_start[], __kuser_helper_end[];
908c2ecf20Sopenharmony_ci	int kuser_sz = __kuser_helper_end - __kuser_helper_start;
918c2ecf20Sopenharmony_ci	unsigned long vpage;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	vpage = get_zeroed_page(GFP_ATOMIC);
948c2ecf20Sopenharmony_ci	if (!vpage)
958c2ecf20Sopenharmony_ci		return -ENOMEM;
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	/* Copy kuser helpers */
988c2ecf20Sopenharmony_ci	memcpy((void *)vpage, __kuser_helper_start, kuser_sz);
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	flush_icache_range(vpage, vpage + KUSER_SIZE);
1018c2ecf20Sopenharmony_ci	kuser_page[0] = virt_to_page(vpage);
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	return 0;
1048c2ecf20Sopenharmony_ci}
1058c2ecf20Sopenharmony_ciarch_initcall(alloc_kuser_page);
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ciint arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
1088c2ecf20Sopenharmony_ci{
1098c2ecf20Sopenharmony_ci	struct mm_struct *mm = current->mm;
1108c2ecf20Sopenharmony_ci	int ret;
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	mmap_write_lock(mm);
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	/* Map kuser helpers to user space address */
1158c2ecf20Sopenharmony_ci	ret = install_special_mapping(mm, KUSER_BASE, KUSER_SIZE,
1168c2ecf20Sopenharmony_ci				      VM_READ | VM_EXEC | VM_MAYREAD |
1178c2ecf20Sopenharmony_ci				      VM_MAYEXEC, kuser_page);
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	mmap_write_unlock(mm);
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	return ret;
1228c2ecf20Sopenharmony_ci}
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ciconst char *arch_vma_name(struct vm_area_struct *vma)
1258c2ecf20Sopenharmony_ci{
1268c2ecf20Sopenharmony_ci	return (vma->vm_start == KUSER_BASE) ? "[kuser]" : NULL;
1278c2ecf20Sopenharmony_ci}
128