1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci 3f08c3bdfSopenharmony_ci/* 4f08c3bdfSopenharmony_ci * Copyright (C) 2018 Intel Corporation 5f08c3bdfSopenharmony_ci * Author: Neri, Ricardo <ricardo.neri@intel.com> 6f08c3bdfSopenharmony_ci * Pengfei, Xu <pengfei.xu@intel.com> 7f08c3bdfSopenharmony_ci */ 8f08c3bdfSopenharmony_ci 9f08c3bdfSopenharmony_ci/* 10f08c3bdfSopenharmony_ci * This test will check if Intel umip(User-Mode Execution Prevention) is 11f08c3bdfSopenharmony_ci * working. 12f08c3bdfSopenharmony_ci * 13f08c3bdfSopenharmony_ci * Intel CPU of ICE lake or newer is required for the test 14f08c3bdfSopenharmony_ci * kconfig requirement:CONFIG_X86_INTEL_UMIP=y 15f08c3bdfSopenharmony_ci */ 16f08c3bdfSopenharmony_ci 17f08c3bdfSopenharmony_ci#define _GNU_SOURCE 18f08c3bdfSopenharmony_ci#include <unistd.h> 19f08c3bdfSopenharmony_ci#include <stdio.h> 20f08c3bdfSopenharmony_ci#include <stdlib.h> 21f08c3bdfSopenharmony_ci#include <string.h> 22f08c3bdfSopenharmony_ci#include <sys/wait.h> 23f08c3bdfSopenharmony_ci#include <signal.h> 24f08c3bdfSopenharmony_ci 25f08c3bdfSopenharmony_ci#include "tst_test.h" 26f08c3bdfSopenharmony_ci#include "tst_safe_stdio.h" 27f08c3bdfSopenharmony_ci 28f08c3bdfSopenharmony_ci#define CPUINFO_FILE "/proc/cpuinfo" 29f08c3bdfSopenharmony_ci 30f08c3bdfSopenharmony_ci#define GDT_LEN 10 31f08c3bdfSopenharmony_ci#define IDT_LEN 10 32f08c3bdfSopenharmony_ci 33f08c3bdfSopenharmony_ci#ifdef __x86_64__ 34f08c3bdfSopenharmony_ci 35f08c3bdfSopenharmony_cistatic void asm_sgdt(void) 36f08c3bdfSopenharmony_ci{ 37f08c3bdfSopenharmony_ci unsigned char val[GDT_LEN]; 38f08c3bdfSopenharmony_ci 39f08c3bdfSopenharmony_ci memset(val, 0, sizeof(val)); 40f08c3bdfSopenharmony_ci tst_res(TINFO, "TEST sgdt, sgdt result save at [%p]", val); 41f08c3bdfSopenharmony_ci asm volatile("sgdt %0\n" : "=m" (val)); 42f08c3bdfSopenharmony_ci exit(0); 43f08c3bdfSopenharmony_ci} 44f08c3bdfSopenharmony_ci 45f08c3bdfSopenharmony_cistatic void asm_sidt(void) 46f08c3bdfSopenharmony_ci{ 47f08c3bdfSopenharmony_ci unsigned char val[IDT_LEN]; 48f08c3bdfSopenharmony_ci 49f08c3bdfSopenharmony_ci memset(val, 0, sizeof(val)); 50f08c3bdfSopenharmony_ci tst_res(TINFO, "TEST sidt, sidt result save at [%p]", val); 51f08c3bdfSopenharmony_ci asm volatile("sidt %0\n" : "=m" (val)); 52f08c3bdfSopenharmony_ci exit(0); 53f08c3bdfSopenharmony_ci} 54f08c3bdfSopenharmony_ci 55f08c3bdfSopenharmony_cistatic void asm_sldt(void) 56f08c3bdfSopenharmony_ci{ 57f08c3bdfSopenharmony_ci unsigned long val; 58f08c3bdfSopenharmony_ci 59f08c3bdfSopenharmony_ci tst_res(TINFO, "TEST sldt, sldt result save at [%p]", &val); 60f08c3bdfSopenharmony_ci asm volatile("sldt %0\n" : "=m" (val)); 61f08c3bdfSopenharmony_ci exit(0); 62f08c3bdfSopenharmony_ci} 63f08c3bdfSopenharmony_ci 64f08c3bdfSopenharmony_cistatic void asm_smsw(void) 65f08c3bdfSopenharmony_ci{ 66f08c3bdfSopenharmony_ci unsigned long val; 67f08c3bdfSopenharmony_ci 68f08c3bdfSopenharmony_ci tst_res(TINFO, "TEST smsw, smsw result save at [%p]", &val); 69f08c3bdfSopenharmony_ci asm volatile("smsw %0\n" : "=m" (val)); 70f08c3bdfSopenharmony_ci exit(0); 71f08c3bdfSopenharmony_ci} 72f08c3bdfSopenharmony_ci 73f08c3bdfSopenharmony_cistatic void asm_str(void) 74f08c3bdfSopenharmony_ci{ 75f08c3bdfSopenharmony_ci unsigned long val; 76f08c3bdfSopenharmony_ci 77f08c3bdfSopenharmony_ci tst_res(TINFO, "TEST str, str result save at [%p]", &val); 78f08c3bdfSopenharmony_ci asm volatile("str %0\n" : "=m" (val)); 79f08c3bdfSopenharmony_ci exit(0); 80f08c3bdfSopenharmony_ci} 81f08c3bdfSopenharmony_ci 82f08c3bdfSopenharmony_cistatic void verify_umip_instruction(unsigned int n) 83f08c3bdfSopenharmony_ci{ 84f08c3bdfSopenharmony_ci int status; 85f08c3bdfSopenharmony_ci pid_t pid; 86f08c3bdfSopenharmony_ci 87f08c3bdfSopenharmony_ci pid = SAFE_FORK(); 88f08c3bdfSopenharmony_ci if (pid == 0) { 89f08c3bdfSopenharmony_ci tst_no_corefile(0); 90f08c3bdfSopenharmony_ci 91f08c3bdfSopenharmony_ci switch (n) { 92f08c3bdfSopenharmony_ci case 0: 93f08c3bdfSopenharmony_ci asm_sgdt(); 94f08c3bdfSopenharmony_ci break; 95f08c3bdfSopenharmony_ci case 1: 96f08c3bdfSopenharmony_ci asm_sidt(); 97f08c3bdfSopenharmony_ci break; 98f08c3bdfSopenharmony_ci case 2: 99f08c3bdfSopenharmony_ci asm_sldt(); 100f08c3bdfSopenharmony_ci break; 101f08c3bdfSopenharmony_ci case 3: 102f08c3bdfSopenharmony_ci asm_smsw(); 103f08c3bdfSopenharmony_ci break; 104f08c3bdfSopenharmony_ci case 4: 105f08c3bdfSopenharmony_ci asm_str(); 106f08c3bdfSopenharmony_ci break; 107f08c3bdfSopenharmony_ci default: 108f08c3bdfSopenharmony_ci tst_brk(TBROK, "Invalid tcase parameter: %d", n); 109f08c3bdfSopenharmony_ci } 110f08c3bdfSopenharmony_ci exit(0); 111f08c3bdfSopenharmony_ci } 112f08c3bdfSopenharmony_ci 113f08c3bdfSopenharmony_ci SAFE_WAITPID(pid, &status, 0); 114f08c3bdfSopenharmony_ci 115f08c3bdfSopenharmony_ci switch (n) { 116f08c3bdfSopenharmony_ci case 0: 117f08c3bdfSopenharmony_ci case 1: 118f08c3bdfSopenharmony_ci case 3: 119f08c3bdfSopenharmony_ci /* after linux kernel v5.4 mainline, 64bit SGDT SIDT SMSW will return 120f08c3bdfSopenharmony_ci dummy value and not trigger SIGSEGV due to kernel code change */ 121f08c3bdfSopenharmony_ci if ((tst_kvercmp(5, 4, 0)) >= 0) { 122f08c3bdfSopenharmony_ci tst_res(TINFO, "Linux kernel version is v5.4 or after than v5.4"); 123f08c3bdfSopenharmony_ci if (WIFSIGNALED(status) && WTERMSIG(status) == SIGSEGV) { 124f08c3bdfSopenharmony_ci tst_res(TFAIL, "Got SIGSEGV"); 125f08c3bdfSopenharmony_ci return; 126f08c3bdfSopenharmony_ci } 127f08c3bdfSopenharmony_ci tst_res(TPASS, "Didn't receive SIGSEGV, child exited with %s", 128f08c3bdfSopenharmony_ci tst_strstatus(status)); 129f08c3bdfSopenharmony_ci return; 130f08c3bdfSopenharmony_ci } else 131f08c3bdfSopenharmony_ci tst_res(TINFO, "Linux kernel version is before than v5.4"); 132f08c3bdfSopenharmony_ci break; 133f08c3bdfSopenharmony_ci case 2: 134f08c3bdfSopenharmony_ci case 4: 135f08c3bdfSopenharmony_ci /* after Linux kernel v5.10 mainline, SLDT and STR will return 136f08c3bdfSopenharmony_ci dummy value and not trigger SIGSEGV due to kernel code change */ 137f08c3bdfSopenharmony_ci if ((tst_kvercmp(5, 10, 0)) >= 0) { 138f08c3bdfSopenharmony_ci tst_res(TINFO, "Linux kernel version is v5.10 or higher"); 139f08c3bdfSopenharmony_ci if (WIFSIGNALED(status) && WTERMSIG(status) == SIGSEGV) { 140f08c3bdfSopenharmony_ci tst_res(TFAIL, "Got SIGSEGV"); 141f08c3bdfSopenharmony_ci return; 142f08c3bdfSopenharmony_ci } 143f08c3bdfSopenharmony_ci tst_res(TPASS, "Didn't receive SIGSEGV, child exited with %s", 144f08c3bdfSopenharmony_ci tst_strstatus(status)); 145f08c3bdfSopenharmony_ci return; 146f08c3bdfSopenharmony_ci } else 147f08c3bdfSopenharmony_ci tst_res(TINFO, "Linux kernel version is earlier than v5.10"); 148f08c3bdfSopenharmony_ci break; 149f08c3bdfSopenharmony_ci } 150f08c3bdfSopenharmony_ci 151f08c3bdfSopenharmony_ci if (WIFSIGNALED(status) && WTERMSIG(status) == SIGSEGV) { 152f08c3bdfSopenharmony_ci tst_res(TPASS, "Got SIGSEGV"); 153f08c3bdfSopenharmony_ci return; 154f08c3bdfSopenharmony_ci } 155f08c3bdfSopenharmony_ci tst_res(TFAIL, "Didn't receive SIGSEGV, child exited with %s", 156f08c3bdfSopenharmony_ci tst_strstatus(status)); 157f08c3bdfSopenharmony_ci} 158f08c3bdfSopenharmony_ci 159f08c3bdfSopenharmony_cistatic void setup(void) 160f08c3bdfSopenharmony_ci{ 161f08c3bdfSopenharmony_ci FILE *fp; 162f08c3bdfSopenharmony_ci char buf[2048]; 163f08c3bdfSopenharmony_ci 164f08c3bdfSopenharmony_ci fp = SAFE_FOPEN(CPUINFO_FILE, "r"); 165f08c3bdfSopenharmony_ci while (!feof(fp)) { 166f08c3bdfSopenharmony_ci if (fgets(buf, sizeof(buf), fp) == NULL) { 167f08c3bdfSopenharmony_ci SAFE_FCLOSE(fp); 168f08c3bdfSopenharmony_ci tst_brk(TCONF, "cpuinfo show: cpu does not support umip"); 169f08c3bdfSopenharmony_ci } 170f08c3bdfSopenharmony_ci 171f08c3bdfSopenharmony_ci if (!strstr(buf, "flags")) 172f08c3bdfSopenharmony_ci continue; 173f08c3bdfSopenharmony_ci 174f08c3bdfSopenharmony_ci if (strstr(buf, "umip")) { 175f08c3bdfSopenharmony_ci tst_res(TINFO, "cpuinfo contains umip, CPU supports umip"); 176f08c3bdfSopenharmony_ci break; 177f08c3bdfSopenharmony_ci } else 178f08c3bdfSopenharmony_ci continue; 179f08c3bdfSopenharmony_ci } 180f08c3bdfSopenharmony_ci 181f08c3bdfSopenharmony_ci SAFE_FCLOSE(fp); 182f08c3bdfSopenharmony_ci} 183f08c3bdfSopenharmony_ci 184f08c3bdfSopenharmony_cistatic struct tst_test test = { 185f08c3bdfSopenharmony_ci .min_kver = "4.1", 186f08c3bdfSopenharmony_ci .setup = setup, 187f08c3bdfSopenharmony_ci .tcnt = 5, 188f08c3bdfSopenharmony_ci .forks_child = 1, 189f08c3bdfSopenharmony_ci .test = verify_umip_instruction, 190f08c3bdfSopenharmony_ci .needs_kconfigs = (const char *[]){ 191f08c3bdfSopenharmony_ci "CONFIG_X86_INTEL_UMIP=y | CONFIG_X86_UMIP=y", 192f08c3bdfSopenharmony_ci NULL 193f08c3bdfSopenharmony_ci }, 194f08c3bdfSopenharmony_ci .needs_root = 1, 195f08c3bdfSopenharmony_ci}; 196f08c3bdfSopenharmony_ci 197f08c3bdfSopenharmony_ci#else 198f08c3bdfSopenharmony_ci 199f08c3bdfSopenharmony_ciTST_TEST_TCONF("Tests needs x86_64 CPU"); 200f08c3bdfSopenharmony_ci 201f08c3bdfSopenharmony_ci#endif /* __x86_64__ */ 202