Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <asm/pgalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <asm/sections.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <as-layout.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <os.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <skas.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) static int init_stub_pte(struct mm_struct *mm, unsigned long proc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 			 unsigned long kernel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 	pgd_t *pgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	p4d_t *p4d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	pud_t *pud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	pmd_t *pmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	pte_t *pte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	pgd = pgd_offset(mm, proc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	p4d = p4d_alloc(mm, pgd, proc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	if (!p4d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	pud = pud_alloc(mm, p4d, proc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	if (!pud)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 		goto out_pud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	pmd = pmd_alloc(mm, pud, proc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	if (!pmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 		goto out_pmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	pte = pte_alloc_map(mm, pmd, proc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	if (!pte)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		goto out_pte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	*pte = mk_pte(virt_to_page(kernel), __pgprot(_PAGE_PRESENT));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	*pte = pte_mkread(*pte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  out_pte:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	pmd_free(mm, pmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50)  out_pmd:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	pud_free(mm, pud);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52)  out_pud:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	p4d_free(mm, p4d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) int init_new_context(struct task_struct *task, struct mm_struct *mm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60)  	struct mm_context *from_mm = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	struct mm_context *to_mm = &mm->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	unsigned long stack = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	int ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	stack = get_zeroed_page(GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	if (stack == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	to_mm->id.stack = stack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	if (current->mm != NULL && current->mm != &init_mm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		from_mm = &current->mm->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	block_signals_trace();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	if (from_mm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		to_mm->id.u.pid = copy_context_skas0(stack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 						     from_mm->id.u.pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	else to_mm->id.u.pid = start_userspace(stack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	unblock_signals_trace();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	if (to_mm->id.u.pid < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		ret = to_mm->id.u.pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	ret = init_new_ldt(to_mm, from_mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		printk(KERN_ERR "init_new_context_skas - init_ldt"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		       " failed, errno = %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94)  out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	if (to_mm->id.stack != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		free_page(to_mm->id.stack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) void uml_setup_stubs(struct mm_struct *mm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	int err, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	ret = init_stub_pte(mm, STUB_CODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 			    (unsigned long) __syscall_stub_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	ret = init_stub_pte(mm, STUB_DATA, mm->context.id.stack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	mm->context.stub_pages[0] = virt_to_page(__syscall_stub_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	mm->context.stub_pages[1] = virt_to_page(mm->context.id.stack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	/* dup_mmap already holds mmap_lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	err = install_special_mapping(mm, STUB_START, STUB_END - STUB_START,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 				      VM_READ | VM_MAYREAD | VM_EXEC |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 				      VM_MAYEXEC | VM_DONTCOPY | VM_PFNMAP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 				      mm->context.stub_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		printk(KERN_ERR "install_special_mapping returned %d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	force_sigsegv(SIGSEGV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) void arch_exit_mmap(struct mm_struct *mm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	pte_t *pte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	pte = virt_to_pte(mm, STUB_CODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	if (pte != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		pte_clear(mm, STUB_CODE, pte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	pte = virt_to_pte(mm, STUB_DATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	if (pte == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	pte_clear(mm, STUB_DATA, pte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) void destroy_context(struct mm_struct *mm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	struct mm_context *mmu = &mm->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	 * If init_new_context wasn't called, this will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	 * zero, resulting in a kill(0), which will result in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	 * whole UML suddenly dying.  Also, cover negative and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	 * 1 cases, since they shouldn't happen either.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	if (mmu->id.u.pid < 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		printk(KERN_ERR "corrupt mm_context - pid = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		       mmu->id.u.pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	os_kill_ptraced_process(mmu->id.u.pid, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	free_page(mmu->id.stack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	free_ldt(mmu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }