xref: /openbmc/linux/arch/nios2/mm/init.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
15ccc6af5SLey Foon Tan /*
25ccc6af5SLey Foon Tan  * Copyright (C) 2013 Altera Corporation
35ccc6af5SLey Foon Tan  * Copyright (C) 2010 Tobias Klauser <tklauser@distanz.ch>
45ccc6af5SLey Foon Tan  * Copyright (C) 2009 Wind River Systems Inc
55ccc6af5SLey Foon Tan  *   Implemented by fredrik.markstrom@gmail.com and ivarholmqvist@gmail.com
65ccc6af5SLey Foon Tan  * Copyright (C) 2004 Microtronix Datacom Ltd
75ccc6af5SLey Foon Tan  *
85ccc6af5SLey Foon Tan  * based on arch/m68k/mm/init.c
95ccc6af5SLey Foon Tan  *
105ccc6af5SLey Foon Tan  * This file is subject to the terms and conditions of the GNU General Public
115ccc6af5SLey Foon Tan  * License. See the file "COPYING" in the main directory of this archive
125ccc6af5SLey Foon Tan  * for more details.
135ccc6af5SLey Foon Tan  */
145ccc6af5SLey Foon Tan 
155ccc6af5SLey Foon Tan #include <linux/signal.h>
165ccc6af5SLey Foon Tan #include <linux/sched.h>
175ccc6af5SLey Foon Tan #include <linux/kernel.h>
185ccc6af5SLey Foon Tan #include <linux/errno.h>
195ccc6af5SLey Foon Tan #include <linux/string.h>
205ccc6af5SLey Foon Tan #include <linux/types.h>
215ccc6af5SLey Foon Tan #include <linux/ptrace.h>
225ccc6af5SLey Foon Tan #include <linux/mman.h>
235ccc6af5SLey Foon Tan #include <linux/mm.h>
245ccc6af5SLey Foon Tan #include <linux/init.h>
255ccc6af5SLey Foon Tan #include <linux/pagemap.h>
2657c8a661SMike Rapoport #include <linux/memblock.h>
275ccc6af5SLey Foon Tan #include <linux/slab.h>
285ccc6af5SLey Foon Tan #include <linux/binfmts.h>
295ccc6af5SLey Foon Tan 
305ccc6af5SLey Foon Tan #include <asm/setup.h>
315ccc6af5SLey Foon Tan #include <asm/page.h>
325ccc6af5SLey Foon Tan #include <asm/sections.h>
335ccc6af5SLey Foon Tan #include <asm/tlb.h>
345ccc6af5SLey Foon Tan #include <asm/mmu_context.h>
355ccc6af5SLey Foon Tan #include <asm/cpuinfo.h>
365ccc6af5SLey Foon Tan #include <asm/processor.h>
375ccc6af5SLey Foon Tan 
385ccc6af5SLey Foon Tan pgd_t *pgd_current;
395ccc6af5SLey Foon Tan 
405ccc6af5SLey Foon Tan /*
415ccc6af5SLey Foon Tan  * paging_init() continues the virtual memory environment setup which
425ccc6af5SLey Foon Tan  * was begun by the code in arch/head.S.
435ccc6af5SLey Foon Tan  * The parameters are pointers to where to stick the starting and ending
445ccc6af5SLey Foon Tan  * addresses of available kernel virtual memory.
455ccc6af5SLey Foon Tan  */
paging_init(void)465ccc6af5SLey Foon Tan void __init paging_init(void)
475ccc6af5SLey Foon Tan {
48fa3354e4SMike Rapoport 	unsigned long max_zone_pfn[MAX_NR_ZONES] = { 0 };
495ccc6af5SLey Foon Tan 
505ccc6af5SLey Foon Tan 	pagetable_init();
515ccc6af5SLey Foon Tan 	pgd_current = swapper_pg_dir;
525ccc6af5SLey Foon Tan 
53fa3354e4SMike Rapoport 	max_zone_pfn[ZONE_NORMAL] = max_mapnr;
545ccc6af5SLey Foon Tan 
555ccc6af5SLey Foon Tan 	/* pass the memory from the bootmem allocator to the main allocator */
56fa3354e4SMike Rapoport 	free_area_init(max_zone_pfn);
575ccc6af5SLey Foon Tan 
585ccc6af5SLey Foon Tan 	flush_dcache_range((unsigned long)empty_zero_page,
595ccc6af5SLey Foon Tan 			(unsigned long)empty_zero_page + PAGE_SIZE);
605ccc6af5SLey Foon Tan }
615ccc6af5SLey Foon Tan 
mem_init(void)625ccc6af5SLey Foon Tan void __init mem_init(void)
635ccc6af5SLey Foon Tan {
645ccc6af5SLey Foon Tan 	unsigned long end_mem   = memory_end; /* this must not include
655ccc6af5SLey Foon Tan 						kernel stack at top */
665ccc6af5SLey Foon Tan 
675ccc6af5SLey Foon Tan 	pr_debug("mem_init: start=%lx, end=%lx\n", memory_start, memory_end);
685ccc6af5SLey Foon Tan 
695ccc6af5SLey Foon Tan 	end_mem &= PAGE_MASK;
705ccc6af5SLey Foon Tan 	high_memory = __va(end_mem);
715ccc6af5SLey Foon Tan 
725ccc6af5SLey Foon Tan 	/* this will put all memory onto the freelists */
73c6ffc5caSMike Rapoport 	memblock_free_all();
745ccc6af5SLey Foon Tan }
755ccc6af5SLey Foon Tan 
mmu_init(void)765ccc6af5SLey Foon Tan void __init mmu_init(void)
775ccc6af5SLey Foon Tan {
785ccc6af5SLey Foon Tan 	flush_tlb_all();
795ccc6af5SLey Foon Tan }
805ccc6af5SLey Foon Tan 
81*a6714e72SMike Rapoport pgd_t swapper_pg_dir[PTRS_PER_PGD] __aligned(PAGE_SIZE);
82bf0dc119SMike Rapoport pte_t invalid_pte_table[PTRS_PER_PTE] __aligned(PAGE_SIZE);
835ccc6af5SLey Foon Tan static struct page *kuser_page[1];
845ccc6af5SLey Foon Tan 
alloc_kuser_page(void)855ccc6af5SLey Foon Tan static int alloc_kuser_page(void)
865ccc6af5SLey Foon Tan {
875ccc6af5SLey Foon Tan 	extern char __kuser_helper_start[], __kuser_helper_end[];
885ccc6af5SLey Foon Tan 	int kuser_sz = __kuser_helper_end - __kuser_helper_start;
895ccc6af5SLey Foon Tan 	unsigned long vpage;
905ccc6af5SLey Foon Tan 
915ccc6af5SLey Foon Tan 	vpage = get_zeroed_page(GFP_ATOMIC);
925ccc6af5SLey Foon Tan 	if (!vpage)
935ccc6af5SLey Foon Tan 		return -ENOMEM;
945ccc6af5SLey Foon Tan 
955ccc6af5SLey Foon Tan 	/* Copy kuser helpers */
965ccc6af5SLey Foon Tan 	memcpy((void *)vpage, __kuser_helper_start, kuser_sz);
975ccc6af5SLey Foon Tan 
985ccc6af5SLey Foon Tan 	flush_icache_range(vpage, vpage + KUSER_SIZE);
995ccc6af5SLey Foon Tan 	kuser_page[0] = virt_to_page(vpage);
1005ccc6af5SLey Foon Tan 
1015ccc6af5SLey Foon Tan 	return 0;
1025ccc6af5SLey Foon Tan }
1035ccc6af5SLey Foon Tan arch_initcall(alloc_kuser_page);
1045ccc6af5SLey Foon Tan 
arch_setup_additional_pages(struct linux_binprm * bprm,int uses_interp)1055ccc6af5SLey Foon Tan int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
1065ccc6af5SLey Foon Tan {
1075ccc6af5SLey Foon Tan 	struct mm_struct *mm = current->mm;
1085ccc6af5SLey Foon Tan 	int ret;
1095ccc6af5SLey Foon Tan 
110d8ed45c5SMichel Lespinasse 	mmap_write_lock(mm);
1115ccc6af5SLey Foon Tan 
1125ccc6af5SLey Foon Tan 	/* Map kuser helpers to user space address */
1135ccc6af5SLey Foon Tan 	ret = install_special_mapping(mm, KUSER_BASE, KUSER_SIZE,
1145ccc6af5SLey Foon Tan 				      VM_READ | VM_EXEC | VM_MAYREAD |
1155ccc6af5SLey Foon Tan 				      VM_MAYEXEC, kuser_page);
1165ccc6af5SLey Foon Tan 
117d8ed45c5SMichel Lespinasse 	mmap_write_unlock(mm);
1185ccc6af5SLey Foon Tan 
1195ccc6af5SLey Foon Tan 	return ret;
1205ccc6af5SLey Foon Tan }
1215ccc6af5SLey Foon Tan 
arch_vma_name(struct vm_area_struct * vma)1225ccc6af5SLey Foon Tan const char *arch_vma_name(struct vm_area_struct *vma)
1235ccc6af5SLey Foon Tan {
1245ccc6af5SLey Foon Tan 	return (vma->vm_start == KUSER_BASE) ? "[kuser]" : NULL;
1255ccc6af5SLey Foon Tan }
12653e2fdeeSAnshuman Khandual 
12753e2fdeeSAnshuman Khandual static const pgprot_t protection_map[16] = {
12853e2fdeeSAnshuman Khandual 	[VM_NONE]					= MKP(0, 0, 0),
12953e2fdeeSAnshuman Khandual 	[VM_READ]					= MKP(0, 0, 1),
13053e2fdeeSAnshuman Khandual 	[VM_WRITE]					= MKP(0, 0, 0),
13153e2fdeeSAnshuman Khandual 	[VM_WRITE | VM_READ]				= MKP(0, 0, 1),
13253e2fdeeSAnshuman Khandual 	[VM_EXEC]					= MKP(1, 0, 0),
13353e2fdeeSAnshuman Khandual 	[VM_EXEC | VM_READ]				= MKP(1, 0, 1),
13453e2fdeeSAnshuman Khandual 	[VM_EXEC | VM_WRITE]				= MKP(1, 0, 0),
13553e2fdeeSAnshuman Khandual 	[VM_EXEC | VM_WRITE | VM_READ]			= MKP(1, 0, 1),
13653e2fdeeSAnshuman Khandual 	[VM_SHARED]					= MKP(0, 0, 0),
13753e2fdeeSAnshuman Khandual 	[VM_SHARED | VM_READ]				= MKP(0, 0, 1),
13853e2fdeeSAnshuman Khandual 	[VM_SHARED | VM_WRITE]				= MKP(0, 1, 0),
13953e2fdeeSAnshuman Khandual 	[VM_SHARED | VM_WRITE | VM_READ]		= MKP(0, 1, 1),
14053e2fdeeSAnshuman Khandual 	[VM_SHARED | VM_EXEC]				= MKP(1, 0, 0),
14153e2fdeeSAnshuman Khandual 	[VM_SHARED | VM_EXEC | VM_READ]			= MKP(1, 0, 1),
14253e2fdeeSAnshuman Khandual 	[VM_SHARED | VM_EXEC | VM_WRITE]		= MKP(1, 1, 0),
14353e2fdeeSAnshuman Khandual 	[VM_SHARED | VM_EXEC | VM_WRITE | VM_READ]	= MKP(1, 1, 1)
14453e2fdeeSAnshuman Khandual };
14553e2fdeeSAnshuman Khandual DECLARE_VM_GET_PAGE_PROT
146