exec_prot_support.c revision 272343
1430SN/A/*      $NetBSD: exec_prot_support.c,v 1.1 2011/07/18 23:16:09 jym Exp $ */
22362SN/A
3430SN/A/*-
4430SN/A * Copyright (c) 2011 The NetBSD Foundation, Inc.
5430SN/A * All rights reserved.
6430SN/A *
72362SN/A * This code is derived from software contributed to The NetBSD Foundation
8430SN/A * by Jean-Yves Migeon.
92362SN/A *
10430SN/A * Redistribution and use in source and binary forms, with or without
11430SN/A * modification, are permitted provided that the following conditions
12430SN/A * are met:
13430SN/A * 1. Redistributions of source code must retain the above copyright
14430SN/A *    notice, this list of conditions and the following disclaimer.
15430SN/A * 2. Redistributions in binary form must reproduce the above copyright
16430SN/A *    notice, this list of conditions and the following disclaimer in the
17430SN/A *    documentation and/or other materials provided with the distribution.
18430SN/A *
19430SN/A * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20430SN/A * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
212362SN/A * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
222362SN/A * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
232362SN/A * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24430SN/A * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25430SN/A * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26430SN/A * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27430SN/A * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28430SN/A * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29430SN/A * POSSIBILITY OF SUCH DAMAGE.
30430SN/A */
31430SN/A
32430SN/A#include <sys/cdefs.h>
33430SN/A__RCSID("$NetBSD: exec_prot_support.c,v 1.1 2011/07/18 23:16:09 jym Exp $");
34430SN/A
35430SN/A#include <stdlib.h>
36430SN/A#include <sys/sysctl.h>
37430SN/A
38430SN/A#include "../../common/exec_prot.h"
39430SN/A
40430SN/A/*
41430SN/A * Support for executable space protection has always been erratic under i386.
42430SN/A * Originally IA-32 can't do per-page execute permission, so it is
43430SN/A * implemented using different executable segments for %cs (code segment).
44430SN/A * This only allows coarse grained protection, especially when memory starts
45430SN/A * being fragmented.
46430SN/A * Later, PAE was introduced together with a NX/XD bit in the page table
47430SN/A * entry to offer per-page permission.
48430SN/A */
49430SN/Aint
50430SN/Aexec_prot_support(void)
51430SN/A{
52430SN/A	int pae;
53430SN/A	size_t pae_len = sizeof(pae);
54430SN/A
55430SN/A	if (sysctlbyname("machdep.pae", &pae, &pae_len, NULL, 0) == -1)
56430SN/A		return PARTIAL_XP;
57430SN/A
58430SN/A	if (pae == 1) {
59430SN/A		if (system("cpuctl identify 0 | grep -q NOX") == 0 ||
60430SN/A		    system("cpuctl identify 0 | grep -q XD") == 0)
61430SN/A			return PERPAGE_XP;
62430SN/A	}
63430SN/A
64430SN/A	return PARTIAL_XP;
65430SN/A}
66430SN/A