cregit-Linux how code gets into the kernel

Release 4.10 tools/testing/selftests/powerpc/primitives/load_unaligned_zeropad.c

/*
 * Userspace test harness for load_unaligned_zeropad. Creates two
 * pages and uses mprotect to prevent access to the second page and
 * a SEGV handler that walks the exception tables and runs the fixup
 * routine.
 *
 * The results are compared against a normal load that is that is
 * performed while access to the second page is enabled via mprotect.
 *
 * Copyright (C) 2014 Anton Blanchard <anton@au.ibm.com>, IBM
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 */

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include <signal.h>
#include <unistd.h>
#include <sys/mman.h>


#define FIXUP_SECTION ".ex_fixup"

static inline unsigned long __fls(unsigned long x);

#include "word-at-a-time.h"

#include "utils.h"


static inline unsigned long __fls(unsigned long x) { int lz; asm (PPC_CNTLZL "%0,%1" : "=r" (lz) : "r" (x)); return sizeof(unsigned long) - 1 - lz; }

Contributors

PersonTokensPropCommitsCommitProp
michael ellermanmichael ellerman27100.00%1100.00%
Total27100.00%1100.00%

static int page_size; static char *mem_region;
static int protect_region(void) { if (mprotect(mem_region + page_size, page_size, PROT_NONE)) { perror("mprotect"); return 1; } return 0; }

Contributors

PersonTokensPropCommitsCommitProp
michael ellermanmichael ellerman34100.00%1100.00%
Total34100.00%1100.00%


static int unprotect_region(void) { if (mprotect(mem_region + page_size, page_size, PROT_READ|PROT_WRITE)) { perror("mprotect"); return 1; } return 0; }

Contributors

PersonTokensPropCommitsCommitProp
michael ellermanmichael ellerman36100.00%1100.00%
Total36100.00%1100.00%

extern char __start___ex_table[]; extern char __stop___ex_table[]; #if defined(__powerpc64__) #define UCONTEXT_NIA(UC) (UC)->uc_mcontext.gp_regs[PT_NIP] #elif defined(__powerpc__) #define UCONTEXT_NIA(UC) (UC)->uc_mcontext.uc_regs->gregs[PT_NIP] #else #error implement UCONTEXT_NIA #endif struct extbl_entry { int insn; int fixup; };
static void segv_handler(int signr, siginfo_t *info, void *ptr) { ucontext_t *uc = (ucontext_t *)ptr; unsigned long addr = (unsigned long)info->si_addr; unsigned long *ip = &UCONTEXT_NIA(uc); struct extbl_entry *entry = (struct extbl_entry *)__start___ex_table; while (entry < (struct extbl_entry *)__stop___ex_table) { unsigned long insn, fixup; insn = (unsigned long)&entry->insn + entry->insn; fixup = (unsigned long)&entry->fixup + entry->fixup; if (insn == *ip) { *ip = fixup; return; } } printf("No exception table match for NIA %lx ADDR %lx\n", *ip, addr); abort(); }

Contributors

PersonTokensPropCommitsCommitProp
michael ellermanmichael ellerman10776.98%266.67%
nick pigginnick piggin3223.02%133.33%
Total139100.00%3100.00%


static void setup_segv_handler(void) { struct sigaction action; memset(&action, 0, sizeof(action)); action.sa_sigaction = segv_handler; action.sa_flags = SA_SIGINFO; sigaction(SIGSEGV, &action, NULL); }

Contributors

PersonTokensPropCommitsCommitProp
michael ellermanmichael ellerman47100.00%1100.00%
Total47100.00%1100.00%


static int do_one_test(char *p, int page_offset) { unsigned long should; unsigned long got; FAIL_IF(unprotect_region()); should = *(unsigned long *)p; FAIL_IF(protect_region()); got = load_unaligned_zeropad(p); if (should != got) { printf("offset %u load_unaligned_zeropad returned 0x%lx, should be 0x%lx\n", page_offset, got, should); return 1; } return 0; }

Contributors

PersonTokensPropCommitsCommitProp
michael ellermanmichael ellerman75100.00%2100.00%
Total75100.00%2100.00%


static int test_body(void) { unsigned long i; page_size = getpagesize(); mem_region = mmap(NULL, page_size * 2, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); FAIL_IF(mem_region == MAP_FAILED); for (i = 0; i < page_size; i++) mem_region[i] = i; memset(mem_region+page_size, 0, page_size); setup_segv_handler(); for (i = 0; i < page_size; i++) FAIL_IF(do_one_test(mem_region+i, i)); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
michael ellermanmichael ellerman110100.00%1100.00%
Total110100.00%1100.00%


int main(void) { return test_harness(test_body, "load_unaligned_zeropad"); }

Contributors

PersonTokensPropCommitsCommitProp
michael ellermanmichael ellerman15100.00%1100.00%
Total15100.00%1100.00%


Overall Contributors

PersonTokensPropCommitsCommitProp
michael ellermanmichael ellerman54692.70%480.00%
nick pigginnick piggin437.30%120.00%
Total589100.00%5100.00%
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.