if.c 9.4 KB

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