blob: 803acbf33eb20dece29e7d0d9f7c71a339780629 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Bjorn Helgaasdf62ab52018-03-09 16:36:33 -06003 * For architectures where we want to allow direct access to the PCI config
4 * stuff - it would probably be preferable on PCs too, but there people
5 * just do it by hand with the magic northbridge registers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/errno.h>
9#include <linux/pci.h>
Matthew Garretteb627e12019-08-19 17:17:47 -070010#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/syscalls.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080012#include <linux/uaccess.h>
Brian Kinge04b0ea2005-09-27 01:21:55 -070013#include "pci.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
Heiko Carstensc4ea37c2009-01-14 14:14:28 +010015SYSCALL_DEFINE5(pciconfig_read, unsigned long, bus, unsigned long, dfn,
16 unsigned long, off, unsigned long, len, void __user *, buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017{
18 struct pci_dev *dev;
19 u8 byte;
20 u16 word;
21 u32 dword;
Krzysztof Wilczyńskid6b17152021-07-29 23:37:55 +000022 int err, cfg_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Krzysztof Wilczyńskia8bd29b2021-07-29 23:37:54 +000024 err = -EPERM;
25 dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 if (!capable(CAP_SYS_ADMIN))
Krzysztof Wilczyńskia8bd29b2021-07-29 23:37:54 +000027 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29 err = -ENODEV;
Sinan Kaya39c94652017-12-19 00:37:53 -050030 dev = pci_get_domain_bus_and_slot(0, bus, dfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 if (!dev)
32 goto error;
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 switch (len) {
35 case 1:
Brian Kinge04b0ea2005-09-27 01:21:55 -070036 cfg_ret = pci_user_read_config_byte(dev, off, &byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 break;
38 case 2:
Brian Kinge04b0ea2005-09-27 01:21:55 -070039 cfg_ret = pci_user_read_config_word(dev, off, &word);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 break;
41 case 4:
Brian Kinge04b0ea2005-09-27 01:21:55 -070042 cfg_ret = pci_user_read_config_dword(dev, off, &dword);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 break;
44 default:
45 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 goto error;
Bjorn Helgaasf7625982013-11-14 11:28:18 -070047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49 err = -EIO;
Heiner Kallweitef9e4002021-01-24 16:39:32 +010050 if (cfg_ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 goto error;
52
53 switch (len) {
54 case 1:
Bjorn Helgaas32e2a3f2021-01-25 11:57:38 -060055 err = put_user(byte, (u8 __user *)buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 break;
57 case 2:
Bjorn Helgaas32e2a3f2021-01-25 11:57:38 -060058 err = put_user(word, (u16 __user *)buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 break;
60 case 4:
Bjorn Helgaas32e2a3f2021-01-25 11:57:38 -060061 err = put_user(dword, (u32 __user *)buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 break;
Alan Coxe4585da2007-04-23 14:57:37 +010063 }
64 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 return err;
66
67error:
68 /* ??? XFree86 doesn't even check the return value. They
69 just look for 0xffffffff in the output, since that's what
70 they get instead of a machine check on x86. */
71 switch (len) {
72 case 1:
Bjorn Helgaas32e2a3f2021-01-25 11:57:38 -060073 put_user(-1, (u8 __user *)buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 break;
75 case 2:
Bjorn Helgaas32e2a3f2021-01-25 11:57:38 -060076 put_user(-1, (u16 __user *)buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 break;
78 case 4:
Bjorn Helgaas32e2a3f2021-01-25 11:57:38 -060079 put_user(-1, (u32 __user *)buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 break;
Alan Coxe4585da2007-04-23 14:57:37 +010081 }
82 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 return err;
84}
85
Heiko Carstensc4ea37c2009-01-14 14:14:28 +010086SYSCALL_DEFINE5(pciconfig_write, unsigned long, bus, unsigned long, dfn,
87 unsigned long, off, unsigned long, len, void __user *, buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
89 struct pci_dev *dev;
90 u8 byte;
91 u16 word;
92 u32 dword;
93 int err = 0;
94
Matthew Garretteb627e12019-08-19 17:17:47 -070095 if (!capable(CAP_SYS_ADMIN) ||
96 security_locked_down(LOCKDOWN_PCI_ACCESS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 return -EPERM;
98
Sinan Kaya39c94652017-12-19 00:37:53 -050099 dev = pci_get_domain_bus_and_slot(0, bus, dfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 if (!dev)
101 return -ENODEV;
102
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400103 switch (len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 case 1:
105 err = get_user(byte, (u8 __user *)buf);
106 if (err)
107 break;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700108 err = pci_user_write_config_byte(dev, off, byte);
Heiner Kallweitef9e4002021-01-24 16:39:32 +0100109 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 err = -EIO;
111 break;
112
113 case 2:
114 err = get_user(word, (u16 __user *)buf);
115 if (err)
116 break;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700117 err = pci_user_write_config_word(dev, off, word);
Heiner Kallweitef9e4002021-01-24 16:39:32 +0100118 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 err = -EIO;
120 break;
121
122 case 4:
123 err = get_user(dword, (u32 __user *)buf);
124 if (err)
125 break;
Brian Kinge04b0ea2005-09-27 01:21:55 -0700126 err = pci_user_write_config_dword(dev, off, dword);
Heiner Kallweitef9e4002021-01-24 16:39:32 +0100127 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 err = -EIO;
129 break;
130
131 default:
132 err = -EINVAL;
133 break;
Alan Coxe4585da2007-04-23 14:57:37 +0100134 }
Alan Coxe4585da2007-04-23 14:57:37 +0100135 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 return err;
137}