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