if.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. #include <linux/capability.h>
  2. #include <linux/seq_file.h>
  3. #include <linux/uaccess.h>
  4. #include <linux/proc_fs.h>
  5. #include <linux/ctype.h>
  6. #include <linux/string.h>
  7. #include <linux/slab.h>
  8. #include <linux/init.h>
  9. #define LINE_SIZE 80
  10. #include <asm/mtrr.h>
  11. #include "mtrr.h"
  12. #define FILE_FCOUNT(f) (((struct seq_file *)((f)->private_data))->private)
  13. static const char *const mtrr_strings[MTRR_NUM_TYPES] =
  14. {
  15. "uncachable", /* 0 */
  16. "write-combining", /* 1 */
  17. "?", /* 2 */
  18. "?", /* 3 */
  19. "write-through", /* 4 */
  20. "write-protect", /* 5 */
  21. "write-back", /* 6 */
  22. };
  23. const char *mtrr_attrib_to_str(int x)
  24. {
  25. return (x <= 6) ? mtrr_strings[x] : "?";
  26. }
  27. #ifdef CONFIG_PROC_FS
  28. static int
  29. mtrr_file_add(unsigned long base, unsigned long size,
  30. unsigned int type, bool increment, struct file *file, int page)
  31. {
  32. unsigned int *fcount = FILE_FCOUNT(file);
  33. int reg, max;
  34. max = num_var_ranges;
  35. if (fcount == NULL) {
  36. fcount = kzalloc(max * sizeof *fcount, GFP_KERNEL);
  37. if (!fcount)
  38. return -ENOMEM;
  39. FILE_FCOUNT(file) = fcount;
  40. }
  41. if (!page) {
  42. if ((base & (PAGE_SIZE - 1)) || (size & (PAGE_SIZE - 1)))
  43. return -EINVAL;
  44. base >>= PAGE_SHIFT;
  45. size >>= PAGE_SHIFT;
  46. }
  47. reg = mtrr_add_page(base, size, type, true);
  48. if (reg >= 0)
  49. ++fcount[reg];
  50. return reg;
  51. }
  52. static int
  53. mtrr_file_del(unsigned long base, unsigned long size,
  54. struct file *file, int page)
  55. {
  56. unsigned int *fcount = FILE_FCOUNT(file);
  57. int reg;
  58. if (!page) {
  59. if ((base & (PAGE_SIZE - 1)) || (size & (PAGE_SIZE - 1)))
  60. return -EINVAL;
  61. base >>= PAGE_SHIFT;
  62. size >>= PAGE_SHIFT;
  63. }
  64. reg = mtrr_del_page(-1, base, size);
  65. if (reg < 0)
  66. return reg;
  67. if (fcount == NULL)
  68. return reg;
  69. if (fcount[reg] < 1)
  70. return -EINVAL;
  71. --fcount[reg];
  72. return reg;
  73. }
  74. /*
  75. * seq_file can seek but we ignore it.
  76. *
  77. * Format of control line:
  78. * "base=%Lx size=%Lx type=%s" or "disable=%d"
  79. */
  80. static ssize_t
  81. mtrr_write(struct file *file, const char __user *buf, size_t len, loff_t * ppos)
  82. {
  83. int i, err;
  84. unsigned long reg;
  85. unsigned long long base, size;
  86. char *ptr;
  87. char line[LINE_SIZE];
  88. int length;
  89. size_t linelen;
  90. if (!capable(CAP_SYS_ADMIN))
  91. return -EPERM;
  92. memset(line, 0, LINE_SIZE);
  93. length = len;
  94. length--;
  95. if (length > LINE_SIZE - 1)
  96. length = LINE_SIZE - 1;
  97. if (length < 0)
  98. return -EINVAL;
  99. if (copy_from_user(line, buf, length))
  100. return -EFAULT;
  101. linelen = strlen(line);
  102. ptr = line + linelen - 1;
  103. if (linelen && *ptr == '\n')
  104. *ptr = '\0';
  105. if (!strncmp(line, "disable=", 8)) {
  106. reg = simple_strtoul(line + 8, &ptr, 0);
  107. err = mtrr_del_page(reg, 0, 0);
  108. if (err < 0)
  109. return err;
  110. return len;
  111. }
  112. if (strncmp(line, "base=", 5))
  113. return -EINVAL;
  114. base = simple_strtoull(line + 5, &ptr, 0);
  115. ptr = skip_spaces(ptr);
  116. if (strncmp(ptr, "size=", 5))
  117. return -EINVAL;
  118. size = simple_strtoull(ptr + 5, &ptr, 0);
  119. if ((base & 0xfff) || (size & 0xfff))
  120. return -EINVAL;
  121. ptr = skip_spaces(ptr);
  122. if (strncmp(ptr, "type=", 5))
  123. return -EINVAL;
  124. ptr = skip_spaces(ptr + 5);
  125. for (i = 0; i < MTRR_NUM_TYPES; ++i) {
  126. if (strcmp(ptr, mtrr_strings[i]))
  127. continue;
  128. base >>= PAGE_SHIFT;
  129. size >>= PAGE_SHIFT;
  130. err = mtrr_add_page((unsigned long)base, (unsigned long)size, i, true);
  131. if (err < 0)
  132. return err;
  133. return len;
  134. }
  135. return -EINVAL;
  136. }
  137. static long
  138. mtrr_ioctl(struct file *file, unsigned int cmd, unsigned long __arg)
  139. {
  140. int err = 0;
  141. mtrr_type type;
  142. unsigned long base;
  143. unsigned long size;
  144. struct mtrr_sentry sentry;
  145. struct mtrr_gentry gentry;
  146. void __user *arg = (void __user *) __arg;
  147. switch (cmd) {
  148. case MTRRIOC_ADD_ENTRY:
  149. case MTRRIOC_SET_ENTRY:
  150. case MTRRIOC_DEL_ENTRY:
  151. case MTRRIOC_KILL_ENTRY:
  152. case MTRRIOC_ADD_PAGE_ENTRY:
  153. case MTRRIOC_SET_PAGE_ENTRY:
  154. case MTRRIOC_DEL_PAGE_ENTRY:
  155. case MTRRIOC_KILL_PAGE_ENTRY:
  156. if (copy_from_user(&sentry, arg, sizeof sentry))
  157. return -EFAULT;
  158. break;
  159. case MTRRIOC_GET_ENTRY:
  160. case MTRRIOC_GET_PAGE_ENTRY:
  161. if (copy_from_user(&gentry, arg, sizeof gentry))
  162. return -EFAULT;
  163. break;
  164. #ifdef CONFIG_COMPAT
  165. case MTRRIOC32_ADD_ENTRY:
  166. case MTRRIOC32_SET_ENTRY:
  167. case MTRRIOC32_DEL_ENTRY:
  168. case MTRRIOC32_KILL_ENTRY:
  169. case MTRRIOC32_ADD_PAGE_ENTRY:
  170. case MTRRIOC32_SET_PAGE_ENTRY:
  171. case MTRRIOC32_DEL_PAGE_ENTRY:
  172. case MTRRIOC32_KILL_PAGE_ENTRY: {
  173. struct mtrr_sentry32 __user *s32;
  174. s32 = (struct mtrr_sentry32 __user *)__arg;
  175. err = get_user(sentry.base, &s32->base);
  176. err |= get_user(sentry.size, &s32->size);
  177. err |= get_user(sentry.type, &s32->type);
  178. if (err)
  179. return err;
  180. break;
  181. }
  182. case MTRRIOC32_GET_ENTRY:
  183. case MTRRIOC32_GET_PAGE_ENTRY: {
  184. struct mtrr_gentry32 __user *g32;
  185. g32 = (struct mtrr_gentry32 __user *)__arg;
  186. err = get_user(gentry.regnum, &g32->regnum);
  187. err |= get_user(gentry.base, &g32->base);
  188. err |= get_user(gentry.size, &g32->size);
  189. err |= get_user(gentry.type, &g32->type);
  190. if (err)
  191. return err;
  192. break;
  193. }
  194. #endif
  195. }
  196. switch (cmd) {
  197. default:
  198. return -ENOTTY;
  199. case MTRRIOC_ADD_ENTRY:
  200. #ifdef CONFIG_COMPAT
  201. case MTRRIOC32_ADD_ENTRY:
  202. #endif
  203. if (!capable(CAP_SYS_ADMIN))
  204. return -EPERM;
  205. err =
  206. mtrr_file_add(sentry.base, sentry.size, sentry.type, true,
  207. file, 0);
  208. break;
  209. case MTRRIOC_SET_ENTRY:
  210. #ifdef CONFIG_COMPAT
  211. case MTRRIOC32_SET_ENTRY:
  212. #endif
  213. if (!capable(CAP_SYS_ADMIN))
  214. return -EPERM;
  215. err = mtrr_add(sentry.base, sentry.size, sentry.type, false);
  216. break;
  217. case MTRRIOC_DEL_ENTRY:
  218. #ifdef CONFIG_COMPAT
  219. case MTRRIOC32_DEL_ENTRY:
  220. #endif
  221. if (!capable(CAP_SYS_ADMIN))
  222. return -EPERM;
  223. err = mtrr_file_del(sentry.base, sentry.size, file, 0);
  224. break;
  225. case MTRRIOC_KILL_ENTRY:
  226. #ifdef CONFIG_COMPAT
  227. case MTRRIOC32_KILL_ENTRY:
  228. #endif
  229. if (!capable(CAP_SYS_ADMIN))
  230. return -EPERM;
  231. err = mtrr_del(-1, sentry.base, sentry.size);
  232. break;
  233. case MTRRIOC_GET_ENTRY:
  234. #ifdef CONFIG_COMPAT
  235. case MTRRIOC32_GET_ENTRY:
  236. #endif
  237. if (gentry.regnum >= num_var_ranges)
  238. return -EINVAL;
  239. mtrr_if->get(gentry.regnum, &base, &size, &type);
  240. /* Hide entries that go above 4GB */
  241. if (base + size - 1 >= (1UL << (8 * sizeof(gentry.size) - PAGE_SHIFT))
  242. || size >= (1UL << (8 * sizeof(gentry.size) - PAGE_SHIFT)))
  243. gentry.base = gentry.size = gentry.type = 0;
  244. else {
  245. gentry.base = base << PAGE_SHIFT;
  246. gentry.size = size << PAGE_SHIFT;
  247. gentry.type = type;
  248. }
  249. break;
  250. case MTRRIOC_ADD_PAGE_ENTRY:
  251. #ifdef CONFIG_COMPAT
  252. case MTRRIOC32_ADD_PAGE_ENTRY:
  253. #endif
  254. if (!capable(CAP_SYS_ADMIN))
  255. return -EPERM;
  256. err =
  257. mtrr_file_add(sentry.base, sentry.size, sentry.type, true,
  258. file, 1);
  259. break;
  260. case MTRRIOC_SET_PAGE_ENTRY:
  261. #ifdef CONFIG_COMPAT
  262. case MTRRIOC32_SET_PAGE_ENTRY:
  263. #endif
  264. if (!capable(CAP_SYS_ADMIN))
  265. return -EPERM;
  266. err =
  267. mtrr_add_page(sentry.base, sentry.size, sentry.type, false);
  268. break;
  269. case MTRRIOC_DEL_PAGE_ENTRY:
  270. #ifdef CONFIG_COMPAT
  271. case MTRRIOC32_DEL_PAGE_ENTRY:
  272. #endif
  273. if (!capable(CAP_SYS_ADMIN))
  274. return -EPERM;
  275. err = mtrr_file_del(sentry.base, sentry.size, file, 1);
  276. break;
  277. case MTRRIOC_KILL_PAGE_ENTRY:
  278. #ifdef CONFIG_COMPAT
  279. case MTRRIOC32_KILL_PAGE_ENTRY:
  280. #endif
  281. if (!capable(CAP_SYS_ADMIN))
  282. return -EPERM;
  283. err = mtrr_del_page(-1, sentry.base, sentry.size);
  284. break;
  285. case MTRRIOC_GET_PAGE_ENTRY:
  286. #ifdef CONFIG_COMPAT
  287. case MTRRIOC32_GET_PAGE_ENTRY:
  288. #endif
  289. if (gentry.regnum >= num_var_ranges)
  290. return -EINVAL;
  291. mtrr_if->get(gentry.regnum, &base, &size, &type);
  292. /* Hide entries that would overflow */
  293. if (size != (__typeof__(gentry.size))size)
  294. gentry.base = gentry.size = gentry.type = 0;
  295. else {
  296. gentry.base = base;
  297. gentry.size = size;
  298. gentry.type = type;
  299. }
  300. break;
  301. }
  302. if (err)
  303. return err;
  304. switch (cmd) {
  305. case MTRRIOC_GET_ENTRY:
  306. case MTRRIOC_GET_PAGE_ENTRY:
  307. if (copy_to_user(arg, &gentry, sizeof gentry))
  308. err = -EFAULT;
  309. break;
  310. #ifdef CONFIG_COMPAT
  311. case MTRRIOC32_GET_ENTRY:
  312. case MTRRIOC32_GET_PAGE_ENTRY: {
  313. struct mtrr_gentry32 __user *g32;
  314. g32 = (struct mtrr_gentry32 __user *)__arg;
  315. err = put_user(gentry.base, &g32->base);
  316. err |= put_user(gentry.size, &g32->size);
  317. err |= put_user(gentry.regnum, &g32->regnum);
  318. err |= put_user(gentry.type, &g32->type);
  319. break;
  320. }
  321. #endif
  322. }
  323. return err;
  324. }
  325. static int mtrr_close(struct inode *ino, struct file *file)
  326. {
  327. unsigned int *fcount = FILE_FCOUNT(file);
  328. int i, max;
  329. if (fcount != NULL) {
  330. max = num_var_ranges;
  331. for (i = 0; i < max; ++i) {
  332. while (fcount[i] > 0) {
  333. mtrr_del(i, 0, 0);
  334. --fcount[i];
  335. }
  336. }
  337. kfree(fcount);
  338. FILE_FCOUNT(file) = NULL;
  339. }
  340. return single_release(ino, file);
  341. }
  342. static int mtrr_seq_show(struct seq_file *seq, void *offset);
  343. static int mtrr_open(struct inode *inode, struct file *file)
  344. {
  345. if (!mtrr_if)
  346. return -EIO;
  347. if (!mtrr_if->get)
  348. return -ENXIO;
  349. return single_open(file, mtrr_seq_show, NULL);
  350. }
  351. static const struct file_operations mtrr_fops = {
  352. .owner = THIS_MODULE,
  353. .open = mtrr_open,
  354. .read = seq_read,
  355. .llseek = seq_lseek,
  356. .write = mtrr_write,
  357. .unlocked_ioctl = mtrr_ioctl,
  358. .compat_ioctl = mtrr_ioctl,
  359. .release = mtrr_close,
  360. };
  361. static int mtrr_seq_show(struct seq_file *seq, void *offset)
  362. {
  363. char factor;
  364. int i, max;
  365. mtrr_type type;
  366. unsigned long base, size;
  367. max = num_var_ranges;
  368. for (i = 0; i < max; i++) {
  369. mtrr_if->get(i, &base, &size, &type);
  370. if (size == 0) {
  371. mtrr_usage_table[i] = 0;
  372. continue;
  373. }
  374. if (size < (0x100000 >> PAGE_SHIFT)) {
  375. /* less than 1MB */
  376. factor = 'K';
  377. size <<= PAGE_SHIFT - 10;
  378. } else {
  379. factor = 'M';
  380. size >>= 20 - PAGE_SHIFT;
  381. }
  382. /* Base can be > 32bit */
  383. seq_printf(seq, "reg%02i: base=0x%06lx000 (%5luMB), size=%5lu%cB, count=%d: %s\n",
  384. i, base, base >> (20 - PAGE_SHIFT),
  385. size, factor,
  386. mtrr_usage_table[i], mtrr_attrib_to_str(type));
  387. }
  388. return 0;
  389. }
  390. static int __init mtrr_if_init(void)
  391. {
  392. struct cpuinfo_x86 *c = &boot_cpu_data;
  393. if ((!cpu_has(c, X86_FEATURE_MTRR)) &&
  394. (!cpu_has(c, X86_FEATURE_K6_MTRR)) &&
  395. (!cpu_has(c, X86_FEATURE_CYRIX_ARR)) &&
  396. (!cpu_has(c, X86_FEATURE_CENTAUR_MCR)))
  397. return -ENODEV;
  398. proc_create("mtrr", S_IWUSR | S_IRUGO, NULL, &mtrr_fops);
  399. return 0;
  400. }
  401. arch_initcall(mtrr_if_init);
  402. #endif /* CONFIG_PROC_FS */