📄 ioperm.c
字号:
DCL_IN_BWX(dense, inl)/* define the "swizzle" switch */static struct ioswtch ioswtch[] = { { jensen_sethae, jensen_outb, jensen_outw, jensen_outl, jensen_inb, jensen_inw, jensen_inl }, { sparse_sethae, sparse_outb, sparse_outw, sparse_outl, sparse_inb, sparse_inw, sparse_inl }, { dense_sethae, dense_outb, dense_outw, dense_outl, dense_inb, dense_inw, dense_inl }};#undef DEBUG_IOPERM/* Routine to process the /proc/cpuinfo information into the fields that are required for correctly determining the platform parameters. */struct cpuinfo_data{ char systype[256]; /* system type field */ char sysvari[256]; /* system variation field */ char cpumodel[256]; /* cpu model field */};static inline intprocess_cpuinfo(struct cpuinfo_data *data){ int got_type, got_vari, got_model; char dummy[256]; FILE * fp; int n; data->systype[0] = 0; data->sysvari[0] = 0; data->cpumodel[0] = 0; /* If there's an /etc/alpha_systype link, we're intending to override whatever's in /proc/cpuinfo. */ n = __readlink (PATH_ALPHA_SYSTYPE, data->systype, 256 - 1); if (n > 0) { data->systype[n] = '\0'; return 1; } fp = fopen (PATH_CPUINFO, "r"); if (!fp) return 0; got_type = got_vari = got_model = 0; while (1) { if (fgets (dummy, 256, fp) == NULL) break; if (!got_type && sscanf (dummy, "system type : %256[^\n]\n", data->systype) == 1) got_type = 1; if (!got_vari && sscanf (dummy, "system variation : %256[^\n]\n", data->sysvari) == 1) got_vari = 1; if (!got_model && sscanf (dummy, "cpu model : %256[^\n]\n", data->cpumodel) == 1) got_model = 1; } fclose (fp);#ifdef DEBUG_IOPERM fprintf(stderr, "system type: `%s'\n", data->systype); fprintf(stderr, "system vari: `%s'\n", data->sysvari); fprintf(stderr, "cpu model: `%s'\n", data->cpumodel);#endif return got_type + got_vari + got_model;}/* * Initialize I/O system. */static intinit_iosys (void){ long addr; int i, olderrno = errno; struct cpuinfo_data data; /* First try the pciconfig_iobase syscall added to 2.2.15 and 2.3.99. */#ifdef __NR_pciconfig_iobase addr = __pciconfig_iobase (IOBASE_DENSE_MEM, 0, 0); if (addr != -1) { ioswizzle_t io_swiz; if (addr == 0) { /* Only Jensen doesn't have dense mem space. */ io.sparse_bus_memory_base = io_system[IOSYS_JENSEN].sparse_bus_mem_base; io.io_base = io_system[IOSYS_JENSEN].bus_io_base; io_swiz = IOSWIZZLE_JENSEN; } else { io.bus_memory_base = addr; addr = __pciconfig_iobase (IOBASE_DENSE_IO, 0, 0); if (addr != 0) { /* The X server uses _bus_base_sparse == 0 to know that BWX access are supported to dense mem space. This is true of every system that supports dense io space, so never fill in io.sparse_bus_memory_base in this case. */ io_swiz = IOSWIZZLE_DENSE; io.io_base = addr; } else { io.sparse_bus_memory_base = __pciconfig_iobase (IOBASE_SPARSE_MEM, 0, 0); io.io_base = __pciconfig_iobase (IOBASE_SPARSE_IO, 0, 0); io_swiz = IOSWIZZLE_SPARSE; } } io.swiz = io_swiz; io.swp = &ioswtch[io_swiz]; return 0; }#endif /* Second, collect the contents of /etc/alpha_systype or /proc/cpuinfo. */ if (process_cpuinfo(&data) == 0) { /* This can happen if the format of /proc/cpuinfo changes. */ fprintf (stderr, "ioperm.init_iosys: Unable to determine system type.\n" "\t(May need " PATH_ALPHA_SYSTYPE " symlink?)\n"); __set_errno (ENODEV); return -1; } /* Translate systype name into i/o system. */ for (i = 0; i < sizeof (platform) / sizeof (platform[0]); ++i) { if (strcmp (platform[i].name, data.systype) == 0) { iosys_t io_sys = platform[i].io_sys; /* Some platforms can have either EV4 or EV5 CPUs. */ if (io_sys == IOSYS_CPUDEP) { /* SABLE or MIKASA or NORITAKE so far. */ if (strcmp (platform[i].name, "Sable") == 0) { if (strncmp (data.cpumodel, "EV4", 3) == 0) io_sys = IOSYS_T2; else if (strncmp (data.cpumodel, "EV5", 3) == 0) io_sys = IOSYS_GAMMA; } else { /* This covers MIKASA/NORITAKE. */ if (strncmp (data.cpumodel, "EV4", 3) == 0) io_sys = IOSYS_APECS; else if (strncmp (data.cpumodel, "EV5", 3) == 0) io_sys = IOSYS_CIA; } if (io_sys == IOSYS_CPUDEP) { /* This can happen if the format of /proc/cpuinfo changes.*/ fprintf (stderr, "ioperm.init_iosys: Unable to determine" " CPU model.\n"); __set_errno (ENODEV); return -1; } } /* Some platforms can have different core logic chipsets */ if (io_sys == IOSYS_PCIDEP) { /* EB164 so far */ if (strcmp (data.systype, "EB164") == 0) { if (strncmp (data.sysvari, "RX164", 5) == 0) io_sys = IOSYS_POLARIS; else if (strncmp (data.sysvari, "LX164", 5) == 0 || strncmp (data.sysvari, "SX164", 5) == 0) io_sys = IOSYS_PYXIS; else io_sys = IOSYS_CIA; } if (io_sys == IOSYS_PCIDEP) { /* This can happen if the format of /proc/cpuinfo changes.*/ fprintf (stderr, "ioperm.init_iosys: Unable to determine" " core logic chipset.\n"); __set_errno (ENODEV); return -1; } } io.bus_memory_base = io_system[io_sys].bus_memory_base; io.sparse_bus_memory_base = io_system[io_sys].sparse_bus_mem_base; io.io_base = io_system[io_sys].bus_io_base; if (io_sys == IOSYS_JENSEN) io.swiz = IOSWIZZLE_JENSEN; else if (io_sys == IOSYS_TSUNAMI || io_sys == IOSYS_POLARIS || io_sys == IOSYS_PYXIS) io.swiz = IOSWIZZLE_DENSE; else io.swiz = IOSWIZZLE_SPARSE; io.swp = &ioswtch[io.swiz]; __set_errno (olderrno); return 0; } } __set_errno (ENODEV); fprintf(stderr, "ioperm.init_iosys: Platform not recognized.\n" "\t(May need " PATH_ALPHA_SYSTYPE " symlink?)\n"); return -1;}int_ioperm (unsigned long int from, unsigned long int num, int turn_on){ unsigned long int addr, len, pagesize = __getpagesize(); int prot; if (!io.swp && init_iosys() < 0) {#ifdef DEBUG_IOPERM fprintf(stderr, "ioperm: init_iosys() failed (%m)\n");#endif return -1; } /* This test isn't as silly as it may look like; consider overflows! */ if (from >= MAX_PORT || from + num > MAX_PORT) { __set_errno (EINVAL);#ifdef DEBUG_IOPERM fprintf(stderr, "ioperm: from/num out of range\n");#endif return -1; }#ifdef DEBUG_IOPERM fprintf(stderr, "ioperm: turn_on %d io.base %ld\n", turn_on, io.base);#endif if (turn_on) { if (!io.base) { int fd; io.hae_cache = 0; if (io.swiz != IOSWIZZLE_DENSE) { /* Synchronize with hw. */ __sethae (0); } fd = __open ("/dev/mem", O_RDWR); if (fd < 0) {#ifdef DEBUG_IOPERM fprintf(stderr, "ioperm: /dev/mem open failed (%m)\n");#endif return -1; } addr = port_to_cpu_addr (0, io.swiz, 1); len = port_to_cpu_addr (MAX_PORT, io.swiz, 1) - addr; io.base = (unsigned long int) __mmap (0, len, PROT_NONE, MAP_SHARED, fd, io.io_base); __close (fd);#ifdef DEBUG_IOPERM fprintf(stderr, "ioperm: mmap of len 0x%lx returned 0x%lx\n", len, io.base);#endif if ((long) io.base == -1) return -1; } prot = PROT_READ | PROT_WRITE; } else { if (!io.base) return 0; /* never was turned on... */ /* turnoff access to relevant pages: */ prot = PROT_NONE; } addr = port_to_cpu_addr (from, io.swiz, 1); addr &= ~(pagesize - 1); len = port_to_cpu_addr (from + num, io.swiz, 1) - addr; return __mprotect ((void *) addr, len, prot);}int_iopl (int level){ switch (level) { case 0: return 0; case 1: case 2: case 3: return _ioperm (0, MAX_PORT, 1); default: __set_errno (EINVAL); return -1; }}void_sethae (unsigned long int addr){ if (!io.swp && init_iosys () < 0) return; io.swp->sethae (addr);}void_outb (unsigned char b, unsigned long int port){ if (port >= MAX_PORT) return; io.swp->outb (b, port);}void_outw (unsigned short b, unsigned long int port){ if (port >= MAX_PORT) return; io.swp->outw (b, port);}void_outl (unsigned int b, unsigned long int port){ if (port >= MAX_PORT) return; io.swp->outl (b, port);}unsigned int_inb (unsigned long int port){ return io.swp->inb (port);}unsigned int_inw (unsigned long int port){ return io.swp->inw (port);}unsigned int_inl (unsigned long int port){ return io.swp->inl (port);}unsigned long int_bus_base(void){ if (!io.swp && init_iosys () < 0) return -1; return io.bus_memory_base;}unsigned long int_bus_base_sparse(void){ if (!io.swp && init_iosys () < 0) return -1; return io.sparse_bus_memory_base;}int_hae_shift(void){ if (!io.swp && init_iosys () < 0) return -1; if (io.swiz == IOSWIZZLE_JENSEN) return 7; if (io.swiz == IOSWIZZLE_SPARSE) return 5; return 0;}weak_alias (_sethae, sethae);weak_alias (_ioperm, ioperm);weak_alias (_iopl, iopl);weak_alias (_inb, inb);weak_alias (_inw, inw);weak_alias (_inl, inl);weak_alias (_outb, outb);weak_alias (_outw, outw);weak_alias (_outl, outl);weak_alias (_bus_base, bus_base);weak_alias (_bus_base_sparse, bus_base_sparse);weak_alias (_hae_shift, hae_shift);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -