mprotect.c revision 0
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi/*
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi * Use is subject to license terms.
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi */
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi#ifdef __sun
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi#pragma ident "@(#)mprotect.c 1.6 05/08/04 SMI"
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi#endif
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi#include <unistd.h>
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi#include <stdlib.h>
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi#include <stdio.h>
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi#include <sys/mman.h>
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi#include <fcntl.h>
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi#include <strings.h>
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi#include "libmicro.h"
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomitypedef volatile char vchar_t;
98bc2ecdbfd4f2f20c3a5e96ae445072fbe22223Aki Tuomi
98bc2ecdbfd4f2f20c3a5e96ae445072fbe22223Aki Tuomitypedef struct {
98bc2ecdbfd4f2f20c3a5e96ae445072fbe22223Aki Tuomi int ts_batch;
98bc2ecdbfd4f2f20c3a5e96ae445072fbe22223Aki Tuomi int ts_res;
98bc2ecdbfd4f2f20c3a5e96ae445072fbe22223Aki Tuomi} tsd_t;
98bc2ecdbfd4f2f20c3a5e96ae445072fbe22223Aki Tuomi
98bc2ecdbfd4f2f20c3a5e96ae445072fbe22223Aki Tuomi#define DEFF "/dev/zero"
98bc2ecdbfd4f2f20c3a5e96ae445072fbe22223Aki Tuomi#define DEFL 8192
98bc2ecdbfd4f2f20c3a5e96ae445072fbe22223Aki Tuomi
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomistatic char *optf = DEFF;
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomistatic long long optl = DEFL;
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomistatic int optr = 0;
03c6532fe7aa3ddae23c99ff6bec78d8dd2e8165Aki Tuomistatic int optw = 0;
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomistatic int opts = 0;
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomistatic int optt = 0;
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomistatic int fd = -1;
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomistatic int anon = 0;
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomistatic int foo = 0;
03c6532fe7aa3ddae23c99ff6bec78d8dd2e8165Aki Tuomistatic vchar_t *seg;
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomistatic int pagesize;
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomiint
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomibenchmark_init()
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi{
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi lm_tsdsize = sizeof (tsd_t);
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi (void) sprintf(lm_optstr, "f:l:rstw");
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi (void) sprintf(lm_usage,
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi " [-f file-to-map (default %s)]\n"
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi " [-l mapping-length (default %d)]\n"
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi " [-r] (read a byte from each page)\n"
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi " [-w] (write a byte on each page)\n"
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi " [-s] (use MAP_SHARED)\n"
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi " [-t] (touch each page after restoring permissions)\n"
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi "notes: measures mprotect()\n",
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi DEFF, DEFL);
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi (void) sprintf(lm_header, "%8s %5s", "size", "flags");
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi return (0);
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi}
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomiint
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomibenchmark_optswitch(int opt, char *optarg)
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi{
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi switch (opt) {
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi case 'f':
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi optf = optarg;
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi anon = strcmp(optf, "MAP_ANON") == 0;
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi break;
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi case 'l':
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi optl = sizetoll(optarg);
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi break;
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi case 'r':
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi optr = 1;
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi break;
03c6532fe7aa3ddae23c99ff6bec78d8dd2e8165Aki Tuomi case 's':
03c6532fe7aa3ddae23c99ff6bec78d8dd2e8165Aki Tuomi opts = 1;
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi break;
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi case 't':
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi optt = 1;
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi break;
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi case 'w':
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi optw = 1;
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi break;
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi default:
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi return (-1);
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi }
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi return (0);
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi}
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomiint
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomibenchmark_initrun()
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi{
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi int flags;
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi int i;
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi if (!anon)
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi fd = open(optf, O_RDWR);
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi flags = opts ? MAP_SHARED : MAP_PRIVATE;
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi flags |= anon ? MAP_ANON : 0;
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi seg = (vchar_t *)mmap(NULL, lm_optB * optl, PROT_READ | PROT_WRITE,
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi flags, anon ? -1 : fd, 0L);
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi if (seg == MAP_FAILED) {
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi return (-1);
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi }
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi if (optr) {
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi for (i = 0; i < lm_optB * optl; i += 4096) {
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi foo += seg[i];
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi }
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi }
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi if (optw) {
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi for (i = 0; i < lm_optB * optl; i += 4096) {
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi seg[i] = 1;
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi }
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi }
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi pagesize = getpagesize();
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi return (0);
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi}
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomiint
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomibenchmark(void *tsd, result_t *res)
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi{
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi tsd_t *ts = (tsd_t *)tsd;
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi int i;
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi int us;
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi int prot = PROT_NONE;
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi int j, k;
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi us = (getpindex() * lm_optT) + gettindex();
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi for (i = 0; i < lm_optB; i++) {
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi switch ((us + ts->ts_batch + i) % 2) {
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi case 0:
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi prot = PROT_NONE;
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi if (optt) {
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi for (j = k = 0; j < optl; j += pagesize)
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi k += seg[i * optl + j];
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi ts->ts_res += k;
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi }
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi break;
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi default:
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi prot = PROT_READ | PROT_WRITE;
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi break;
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi }
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi if (mprotect((void *)&seg[i * optl], optl, prot) == -1) {
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi res->re_errors++;
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi }
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi }
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi res->re_count += lm_optB;
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi ts->ts_batch++;
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi return (0);
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi}
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi
98bc2ecdbfd4f2f20c3a5e96ae445072fbe22223Aki Tuomichar *
98bc2ecdbfd4f2f20c3a5e96ae445072fbe22223Aki Tuomibenchmark_result()
98bc2ecdbfd4f2f20c3a5e96ae445072fbe22223Aki Tuomi{
98bc2ecdbfd4f2f20c3a5e96ae445072fbe22223Aki Tuomi static char result[256];
98bc2ecdbfd4f2f20c3a5e96ae445072fbe22223Aki Tuomi char flags[6];
98bc2ecdbfd4f2f20c3a5e96ae445072fbe22223Aki Tuomi
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi flags[0] = anon ? 'a' : '-';
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi flags[1] = optr ? 'r' : '-';
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi flags[2] = optw ? 'w' : '-';
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi flags[3] = opts ? 's' : '-';
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi flags[4] = optt ? 't' : '-';
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi flags[5] = 0;
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi (void) sprintf(result, "%8lld %5s", optl, flags);
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi return (result);
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi}
4036c1ca99d2c517f68a5b67a419ae7fdfd45300Aki Tuomi