Path: blob/master/tools/testing/selftests/ia64/aliasing-test.c
26302 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* Exercise /dev/mem mmap cases that have been troublesome in the past3*4* (c) Copyright 2007 Hewlett-Packard Development Company, L.P.5* Bjorn Helgaas <[email protected]>6*/78#include <stdlib.h>9#include <stdio.h>10#include <sys/types.h>11#include <dirent.h>12#include <fcntl.h>13#include <fnmatch.h>14#include <string.h>15#include <sys/ioctl.h>16#include <sys/mman.h>17#include <sys/stat.h>18#include <unistd.h>19#include <linux/pci.h>2021int sum;2223static int map_mem(char *path, off_t offset, size_t length, int touch)24{25int fd, rc;26void *addr;27int *c;2829fd = open(path, O_RDWR);30if (fd == -1) {31perror(path);32return -1;33}3435if (fnmatch("/proc/bus/pci/*", path, 0) == 0) {36rc = ioctl(fd, PCIIOC_MMAP_IS_MEM);37if (rc == -1)38perror("PCIIOC_MMAP_IS_MEM ioctl");39}4041addr = mmap(NULL, length, PROT_READ|PROT_WRITE, MAP_SHARED, fd, offset);42if (addr == MAP_FAILED)43return 1;4445if (touch) {46c = (int *) addr;47while (c < (int *) (addr + length))48sum += *c++;49}5051rc = munmap(addr, length);52if (rc == -1) {53perror("munmap");54return -1;55}5657close(fd);58return 0;59}6061static int scan_tree(char *path, char *file, off_t offset, size_t length, int touch)62{63struct dirent **namelist;64char *name, *path2;65int i, n, r, rc = 0, result = 0;66struct stat buf;6768n = scandir(path, &namelist, 0, alphasort);69if (n < 0) {70perror("scandir");71return -1;72}7374for (i = 0; i < n; i++) {75name = namelist[i]->d_name;7677if (fnmatch(".", name, 0) == 0)78goto skip;79if (fnmatch("..", name, 0) == 0)80goto skip;8182path2 = malloc(strlen(path) + strlen(name) + 3);83strcpy(path2, path);84strcat(path2, "/");85strcat(path2, name);8687if (fnmatch(file, name, 0) == 0) {88rc = map_mem(path2, offset, length, touch);89if (rc == 0)90fprintf(stderr, "PASS: %s 0x%lx-0x%lx is %s\n", path2, offset, offset + length, touch ? "readable" : "mappable");91else if (rc > 0)92fprintf(stderr, "PASS: %s 0x%lx-0x%lx not mappable\n", path2, offset, offset + length);93else {94fprintf(stderr, "FAIL: %s 0x%lx-0x%lx not accessible\n", path2, offset, offset + length);95return rc;96}97} else {98r = lstat(path2, &buf);99if (r == 0 && S_ISDIR(buf.st_mode)) {100rc = scan_tree(path2, file, offset, length, touch);101if (rc < 0)102return rc;103}104}105106result |= rc;107free(path2);108109skip:110free(namelist[i]);111}112free(namelist);113return result;114}115116char buf[1024];117118static int read_rom(char *path)119{120int fd, rc;121size_t size = 0;122123fd = open(path, O_RDWR);124if (fd == -1) {125perror(path);126return -1;127}128129rc = write(fd, "1", 2);130if (rc <= 0) {131close(fd);132perror("write");133return -1;134}135136do {137rc = read(fd, buf, sizeof(buf));138if (rc > 0)139size += rc;140} while (rc > 0);141142close(fd);143return size;144}145146static int scan_rom(char *path, char *file)147{148struct dirent **namelist;149char *name, *path2;150int i, n, r, rc = 0, result = 0;151struct stat buf;152153n = scandir(path, &namelist, 0, alphasort);154if (n < 0) {155perror("scandir");156return -1;157}158159for (i = 0; i < n; i++) {160name = namelist[i]->d_name;161162if (fnmatch(".", name, 0) == 0)163goto skip;164if (fnmatch("..", name, 0) == 0)165goto skip;166167path2 = malloc(strlen(path) + strlen(name) + 3);168strcpy(path2, path);169strcat(path2, "/");170strcat(path2, name);171172if (fnmatch(file, name, 0) == 0) {173rc = read_rom(path2);174175/*176* It's OK if the ROM is unreadable. Maybe there177* is no ROM, or some other error occurred. The178* important thing is that no MCA happened.179*/180if (rc > 0)181fprintf(stderr, "PASS: %s read %d bytes\n", path2, rc);182else {183fprintf(stderr, "PASS: %s not readable\n", path2);184return rc;185}186} else {187r = lstat(path2, &buf);188if (r == 0 && S_ISDIR(buf.st_mode)) {189rc = scan_rom(path2, file);190if (rc < 0)191return rc;192}193}194195result |= rc;196free(path2);197198skip:199free(namelist[i]);200}201free(namelist);202return result;203}204205int main(void)206{207int rc;208209if (map_mem("/dev/mem", 0, 0xA0000, 1) == 0)210fprintf(stderr, "PASS: /dev/mem 0x0-0xa0000 is readable\n");211else212fprintf(stderr, "FAIL: /dev/mem 0x0-0xa0000 not accessible\n");213214/*215* It's not safe to blindly read the VGA frame buffer. If you know216* how to poke the card the right way, it should respond, but it's217* not safe in general. Many machines, e.g., Intel chipsets, cover218* up a non-responding card by just returning -1, but others will219* report the failure as a machine check.220*/221if (map_mem("/dev/mem", 0xA0000, 0x20000, 0) == 0)222fprintf(stderr, "PASS: /dev/mem 0xa0000-0xc0000 is mappable\n");223else224fprintf(stderr, "FAIL: /dev/mem 0xa0000-0xc0000 not accessible\n");225226if (map_mem("/dev/mem", 0xC0000, 0x40000, 1) == 0)227fprintf(stderr, "PASS: /dev/mem 0xc0000-0x100000 is readable\n");228else229fprintf(stderr, "FAIL: /dev/mem 0xc0000-0x100000 not accessible\n");230231/*232* Often you can map all the individual pieces above (0-0xA0000,233* 0xA0000-0xC0000, and 0xC0000-0x100000), but can't map the whole234* thing at once. This is because the individual pieces use different235* attributes, and there's no single attribute supported over the236* whole region.237*/238rc = map_mem("/dev/mem", 0, 1024*1024, 0);239if (rc == 0)240fprintf(stderr, "PASS: /dev/mem 0x0-0x100000 is mappable\n");241else if (rc > 0)242fprintf(stderr, "PASS: /dev/mem 0x0-0x100000 not mappable\n");243else244fprintf(stderr, "FAIL: /dev/mem 0x0-0x100000 not accessible\n");245246scan_tree("/sys/class/pci_bus", "legacy_mem", 0, 0xA0000, 1);247scan_tree("/sys/class/pci_bus", "legacy_mem", 0xA0000, 0x20000, 0);248scan_tree("/sys/class/pci_bus", "legacy_mem", 0xC0000, 0x40000, 1);249scan_tree("/sys/class/pci_bus", "legacy_mem", 0, 1024*1024, 0);250251scan_rom("/sys/devices", "rom");252253scan_tree("/proc/bus/pci", "??.?", 0, 0xA0000, 1);254scan_tree("/proc/bus/pci", "??.?", 0xA0000, 0x20000, 0);255scan_tree("/proc/bus/pci", "??.?", 0xC0000, 0x40000, 1);256scan_tree("/proc/bus/pci", "??.?", 0, 1024*1024, 0);257258return rc;259}260261262