Catch and recover from yet another linux kernel bug in mprotect.
[platform/upstream/libpciaccess.git] / src / linux_sysfs.c
1 /*
2  * (C) Copyright IBM Corporation 2006
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * on the rights to use, copy, modify, merge, publish, distribute, sub
9  * license, and/or sell copies of the Software, and to permit persons to whom
10  * the Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
19  * IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24
25 /**
26  * \file linux_sysfs.c
27  * Access PCI subsystem using Linux's sysfs interface.  This interface is
28  * available starting somewhere in the late 2.5.x kernel phase, and is the
29  * prefered method on all 2.6.x kernels.
30  *
31  * \author Ian Romanick <idr@us.ibm.com>
32  */
33
34 #define _GNU_SOURCE
35
36 #include <stdlib.h>
37 #include <string.h>
38 #include <stdio.h>
39 #include <unistd.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <fcntl.h>
43 #include <sys/mman.h>
44 #include <dirent.h>
45 #include <errno.h>
46
47 #include "config.h"
48
49 #ifdef HAVE_MTRR
50 #include <asm/mtrr.h>
51 #include <sys/ioctl.h>
52 #endif
53
54 #include "pciaccess.h"
55 #include "pciaccess_private.h"
56 #include "linux_devmem.h"
57
58 static void pci_device_linux_sysfs_enable(struct pci_device *dev);
59
60 static int pci_device_linux_sysfs_read_rom( struct pci_device * dev,
61     void * buffer );
62
63 static int pci_device_linux_sysfs_probe( struct pci_device * dev );
64
65 static int pci_device_linux_sysfs_map_range(struct pci_device *dev,
66     struct pci_device_mapping *map);
67
68 static int pci_device_linux_sysfs_unmap_range(struct pci_device *dev,
69     struct pci_device_mapping *map);
70
71 static int pci_device_linux_sysfs_read( struct pci_device * dev, void * data,
72     pciaddr_t offset, pciaddr_t size, pciaddr_t * bytes_read );
73
74 static int pci_device_linux_sysfs_write( struct pci_device * dev,
75     const void * data, pciaddr_t offset, pciaddr_t size,
76     pciaddr_t * bytes_wrtten );
77
78 static const struct pci_system_methods linux_sysfs_methods = {
79     .destroy = NULL,
80     .destroy_device = NULL,
81     .read_rom = pci_device_linux_sysfs_read_rom,
82     .probe = pci_device_linux_sysfs_probe,
83     .map_range = pci_device_linux_sysfs_map_range,
84     .unmap_range = pci_device_linux_sysfs_unmap_range,
85
86     .read = pci_device_linux_sysfs_read,
87     .write = pci_device_linux_sysfs_write,
88
89     .fill_capabilities = pci_fill_capabilities_generic,
90     .enable = pci_device_linux_sysfs_enable,
91 };
92
93 #define SYS_BUS_PCI "/sys/bus/pci/devices"
94
95
96 static int populate_entries(struct pci_system * pci_sys);
97
98
99 /**
100  * Attempt to access PCI subsystem using Linux's sysfs interface.
101  */
102 _pci_hidden int
103 pci_system_linux_sysfs_create( void )
104 {
105     int err = 0;
106     struct stat st;
107
108
109     /* If the directory "/sys/bus/pci/devices" exists, then the PCI subsystem
110      * can be accessed using this interface.
111      */
112     
113     if ( stat( SYS_BUS_PCI, & st ) == 0 ) {
114         pci_sys = calloc( 1, sizeof( struct pci_system ) );
115         if ( pci_sys != NULL ) {
116             pci_sys->methods = & linux_sysfs_methods;
117 #ifdef HAVE_MTRR
118             pci_sys->mtrr_fd = open("/proc/mtrr", O_WRONLY);
119 #endif
120             err = populate_entries(pci_sys);
121         }
122         else {
123             err = ENOMEM;
124         }
125     }
126     else {
127         err = errno;
128     }
129
130     return err;
131 }
132
133
134 /**
135  * Filter out the names "." and ".." from the scanned sysfs entries.
136  *
137  * \param d  Directory entry being processed by \c scandir.
138  *
139  * \return
140  * Zero if the entry name matches either "." or "..", non-zero otherwise.
141  *
142  * \sa scandir, populate_entries
143  */
144 static int
145 scan_sys_pci_filter( const struct dirent * d )
146 {
147     return !((strcmp( d->d_name, "." ) == 0) 
148              || (strcmp( d->d_name, ".." ) == 0));
149 }
150
151
152 int
153 populate_entries( struct pci_system * p )
154 {
155     struct dirent ** devices;
156     int n;
157     int i;
158     int err = 0;
159
160
161     n = scandir( SYS_BUS_PCI, & devices, scan_sys_pci_filter, alphasort );
162     if ( n > 0 ) {
163         p->num_devices = n;
164         p->devices = calloc( n, sizeof( struct pci_device_private ) );
165
166         if (p->devices != NULL) {
167             for (i = 0 ; i < n ; i++) {
168                 uint8_t config[48];
169                 pciaddr_t bytes;
170                 unsigned dom, bus, dev, func;
171                 struct pci_device_private *device =
172                         (struct pci_device_private *) &p->devices[i];
173
174
175                 sscanf(devices[i]->d_name, "%04x:%02x:%02x.%1u",
176                        & dom, & bus, & dev, & func);
177
178                 device->base.domain = dom;
179                 device->base.bus = bus;
180                 device->base.dev = dev;
181                 device->base.func = func;
182
183
184                 err = pci_device_linux_sysfs_read(& device->base, config, 0,
185                                                   48, & bytes);
186                 if ((bytes == 48) && !err) {
187                     device->base.vendor_id = (uint16_t)config[0]
188                         + ((uint16_t)config[1] << 8);
189                     device->base.device_id = (uint16_t)config[2]
190                         + ((uint16_t)config[3] << 8);
191                     device->base.device_class = (uint32_t)config[9]
192                         + ((uint32_t)config[10] << 8)
193                         + ((uint32_t)config[11] << 16);
194                     device->base.revision = config[8];
195                     device->base.subvendor_id = (uint16_t)config[44]
196                         + ((uint16_t)config[45] << 8);
197                     device->base.subdevice_id = (uint16_t)config[46]
198                         + ((uint16_t)config[47] << 8);
199                 }
200
201                 if (err) {
202                     break;
203                 }
204             }
205         }
206         else {
207             err = ENOMEM;
208         }
209     }
210
211     if (err) {
212         free(p->devices);
213         p->devices = NULL;
214     }
215
216     return err;
217 }
218
219
220 static int
221 pci_device_linux_sysfs_probe( struct pci_device * dev )
222 {
223     char     name[256];
224     uint8_t  config[256];
225     char     resource[512];
226     int fd;
227     pciaddr_t bytes;
228     unsigned i;
229     int err;
230
231
232     err = pci_device_linux_sysfs_read( dev, config, 0, 256, & bytes );
233     if ( bytes >= 64 ) {
234         struct pci_device_private *priv = (struct pci_device_private *) dev;
235
236         dev->irq = config[60];
237         priv->header_type = config[14];
238
239
240         /* The PCI config registers can be used to obtain information
241          * about the memory and I/O regions for the device.  However,
242          * doing so requires some tricky parsing (to correctly handle
243          * 64-bit memory regions) and requires writing to the config
244          * registers.  Since we'd like to avoid having to deal with the
245          * parsing issues and non-root users can write to PCI config
246          * registers, we use a different file in the device's sysfs
247          * directory called "resource".
248          * 
249          * The resource file contains all of the needed information in
250          * a format that is consistent across all platforms.  Each BAR
251          * and the expansion ROM have a single line of data containing
252          * 3, 64-bit hex values:  the first address in the region,
253          * the last address in the region, and the region's flags.
254          */
255         snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/resource",
256                   SYS_BUS_PCI,
257                   dev->domain,
258                   dev->bus,
259                   dev->dev,
260                   dev->func );
261         fd = open( name, O_RDONLY );
262         if ( fd != -1 ) {
263             char * next;
264             pciaddr_t  low_addr;
265             pciaddr_t  high_addr;
266             pciaddr_t  flags;
267
268
269             bytes = read( fd, resource, 512 );
270             resource[511] = '\0';
271
272             close( fd );
273
274             next = resource;
275             for ( i = 0 ; i < 6 ; i++ ) {
276
277                 dev->regions[i].base_addr = strtoull( next, & next, 16 );
278                 high_addr = strtoull( next, & next, 16 );
279                 flags = strtoull( next, & next, 16 );
280                     
281                 if ( dev->regions[i].base_addr != 0 ) {
282                     dev->regions[i].size = (high_addr 
283                                             - dev->regions[i].base_addr) + 1;
284
285                     dev->regions[i].is_IO = (flags & 0x01);
286                     dev->regions[i].is_64 = (flags & 0x04);
287                     dev->regions[i].is_prefetchable = (flags & 0x08);
288                 }
289             }
290
291             low_addr = strtoull( next, & next, 16 );
292             high_addr = strtoull( next, & next, 16 );
293             flags = strtoull( next, & next, 16 );
294             if ( low_addr != 0 ) {
295                 priv->rom_base = low_addr;
296                 dev->rom_size = (high_addr - low_addr) + 1;
297             }
298         }
299     }
300
301     return err;
302 }
303
304
305 static int
306 pci_device_linux_sysfs_read_rom( struct pci_device * dev, void * buffer )
307 {
308     char name[256];
309     int fd;
310     struct stat  st;
311     int err = 0;
312     size_t rom_size;
313     size_t total_bytes;
314
315
316     snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/rom",
317               SYS_BUS_PCI,
318               dev->domain,
319               dev->bus,
320               dev->dev,
321               dev->func );
322     
323     fd = open( name, O_RDWR );
324     if ( fd == -1 ) {
325         /* If reading the ROM using sysfs fails, fall back to the old
326          * /dev/mem based interface.
327          */
328         return pci_device_linux_devmem_read_rom(dev, buffer);
329     }
330
331
332     if ( fstat( fd, & st ) == -1 ) {
333         close( fd );
334         return errno;
335     }
336
337     rom_size = st.st_size;
338     if ( rom_size == 0 )
339         rom_size = 0x10000;
340
341     /* This is a quirky thing on Linux.  Even though the ROM and the file
342      * for the ROM in sysfs are read-only, the string "1" must be written to
343      * the file to enable the ROM.  After the data has been read, "0" must be
344      * written to the file to disable the ROM.
345      */
346     write( fd, "1", 1 );
347     lseek( fd, 0, SEEK_SET );
348
349     for ( total_bytes = 0 ; total_bytes < rom_size ; /* empty */ ) {
350         const int bytes = read( fd, (char *) buffer + total_bytes,
351                                 rom_size - total_bytes );
352         if ( bytes == -1 ) {
353             err = errno;
354             break;
355         }
356         else if ( bytes == 0 ) {
357             break;
358         }
359
360         total_bytes += bytes;
361     }
362         
363
364     lseek( fd, 0, SEEK_SET );
365     write( fd, "0", 1 );
366
367     close( fd );
368     return err;
369 }
370
371
372 static int
373 pci_device_linux_sysfs_read( struct pci_device * dev, void * data,
374                              pciaddr_t offset, pciaddr_t size,
375                              pciaddr_t * bytes_read )
376 {
377     char name[256];
378     pciaddr_t temp_size = size;
379     int err = 0;
380     int fd;
381     char *data_bytes = data;
382
383     if ( bytes_read != NULL ) {
384         *bytes_read = 0;
385     }
386
387     /* Each device has a directory under sysfs.  Within that directory there
388      * is a file named "config".  This file used to access the PCI config
389      * space.  It is used here to obtain most of the information about the
390      * device.
391      */
392     snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/config",
393               SYS_BUS_PCI,
394               dev->domain,
395               dev->bus,
396               dev->dev,
397               dev->func );
398
399     fd = open( name, O_RDONLY );
400     if ( fd == -1 ) {
401         return errno;
402     }
403
404
405     while ( temp_size > 0 ) {
406         const ssize_t bytes = pread64( fd, data_bytes, temp_size, offset );
407
408         /* If zero bytes were read, then we assume it's the end of the
409          * config file.
410          */
411         if ( bytes <= 0 ) {
412             err = errno;
413             break;
414         }
415
416         temp_size -= bytes;
417         offset += bytes;
418         data_bytes += bytes;
419     }
420     
421     if ( bytes_read != NULL ) {
422         *bytes_read = size - temp_size;
423     }
424
425     close( fd );
426     return err;
427 }
428
429
430 static int
431 pci_device_linux_sysfs_write( struct pci_device * dev, const void * data,
432                              pciaddr_t offset, pciaddr_t size,
433                              pciaddr_t * bytes_written )
434 {
435     char name[256];
436     pciaddr_t temp_size = size;
437     int err = 0;
438     int fd;
439     const char *data_bytes = data;
440
441     if ( bytes_written != NULL ) {
442         *bytes_written = 0;
443     }
444
445     /* Each device has a directory under sysfs.  Within that directory there
446      * is a file named "config".  This file used to access the PCI config
447      * space.  It is used here to obtain most of the information about the
448      * device.
449      */
450     snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/config",
451               SYS_BUS_PCI,
452               dev->domain,
453               dev->bus,
454               dev->dev,
455               dev->func );
456
457     fd = open( name, O_WRONLY );
458     if ( fd == -1 ) {
459         return errno;
460     }
461
462
463     while ( temp_size > 0 ) {
464         const ssize_t bytes = pwrite64( fd, data_bytes, temp_size, offset );
465
466         /* If zero bytes were written, then we assume it's the end of the
467          * config file.
468          */
469         if ( bytes <= 0 ) {
470             err = errno;
471             break;
472         }
473
474         temp_size -= bytes;
475         offset += bytes;
476         data_bytes += bytes;
477     }
478     
479     if ( bytes_written != NULL ) {
480         *bytes_written = size - temp_size;
481     }
482
483     close( fd );
484     return err;
485 }
486
487
488 /**
489  * Map a memory region for a device using the Linux sysfs interface.
490  * 
491  * \param dev   Device whose memory region is to be mapped.
492  * \param map   Parameters of the mapping that is to be created.
493  * 
494  * \return
495  * Zero on success or an \c errno value on failure.
496  *
497  * \sa pci_device_map_rrange, pci_device_linux_sysfs_unmap_range
498  *
499  * \todo
500  * Some older 2.6.x kernels don't implement the resourceN files.  On those
501  * systems /dev/mem must be used.  On these systems it is also possible that
502  * \c mmap64 may need to be used.
503  */
504 static int
505 pci_device_linux_sysfs_map_range(struct pci_device *dev,
506                                  struct pci_device_mapping *map)
507 {
508     char name[256];
509     int fd;
510     int err = 0;
511     const int prot = ((map->flags & PCI_DEV_MAP_FLAG_WRITABLE) != 0) 
512         ? (PROT_READ | PROT_WRITE) : PROT_READ;
513     const int open_flags = ((map->flags & PCI_DEV_MAP_FLAG_WRITABLE) != 0) 
514         ? O_RDWR : O_RDONLY;
515     const off_t offset = map->base - dev->regions[map->region].base_addr;
516 #ifdef HAVE_MTRR
517     struct mtrr_sentry sentry = {
518         .base = map->base,
519         .size = map->size,
520         .type = MTRR_TYPE_UNCACHABLE
521     };
522 #endif
523
524     snprintf(name, 255, "%s/%04x:%02x:%02x.%1u/resource%u",
525              SYS_BUS_PCI,
526              dev->domain,
527              dev->bus,
528              dev->dev,
529              dev->func,
530              map->region);
531
532     fd = open(name, open_flags);
533     if (fd == -1) {
534         return errno;
535     }
536
537
538     map->memory = mmap(NULL, map->size, prot, MAP_SHARED, fd, offset);
539     if (map->memory == MAP_FAILED) {
540         map->memory = NULL;
541         close(fd);
542         return errno;
543     }
544
545 #ifdef HAVE_MTRR
546     if ((map->flags & PCI_DEV_MAP_FLAG_CACHABLE) != 0) {
547         sentry.type = MTRR_TYPE_WRBACK;
548     } else if ((map->flags & PCI_DEV_MAP_FLAG_WRITE_COMBINE) != 0) {
549         sentry.type = MTRR_TYPE_WRCOMB;
550     }
551
552     if (pci_sys->mtrr_fd != -1 && sentry.type != MTRR_TYPE_UNCACHABLE) {
553         if (ioctl(pci_sys->mtrr_fd, MTRRIOC_ADD_ENTRY, &sentry) < 0) {
554             /* FIXME: Should we report an error in this case?
555              */
556             fprintf(stderr, "error setting MTRR "
557                     "(base = 0x%08lx, size = 0x%08x, type = %u) %s (%d)\n",
558                     sentry.base, sentry.size, sentry.type,
559                     strerror(errno), errno);
560 /*            err = errno;*/
561         }
562         /* KLUDGE ALERT -- rewrite the PTEs to turn off the CD and WT bits */
563         mprotect (map->memory, map->size, PROT_NONE);
564         err = mprotect (map->memory, map->size, PROT_READ|PROT_WRITE);
565
566         if (err != 0) {
567             fprintf(stderr, "mprotect(PROT_READ | PROT_WRITE) failed: %s\n",
568                     strerror(errno));
569             fprintf(stderr, "remapping without mprotect performace kludge.\n");
570
571             munmap(map->memory, map->size);
572             map->memory = mmap(NULL, map->size, prot, MAP_SHARED, fd, offset);
573             if (map->memory == MAP_FAILED) {
574                 map->memory = NULL;
575                 close(fd);
576                 return errno;
577             }
578         }
579     }
580 #endif
581
582     close(fd);
583
584     return 0;
585 }
586
587 /**
588  * Unmap a memory region for a device using the Linux sysfs interface.
589  * 
590  * \param dev   Device whose memory region is to be unmapped.
591  * \param map   Parameters of the mapping that is to be destroyed.
592  * 
593  * \return
594  * Zero on success or an \c errno value on failure.
595  *
596  * \sa pci_device_map_rrange, pci_device_linux_sysfs_map_range
597  *
598  * \todo
599  * Some older 2.6.x kernels don't implement the resourceN files.  On those
600  * systems /dev/mem must be used.  On these systems it is also possible that
601  * \c mmap64 may need to be used.
602  */
603 static int
604 pci_device_linux_sysfs_unmap_range(struct pci_device *dev,
605                                    struct pci_device_mapping *map)
606 {
607     int err = 0;
608 #ifdef HAVE_MTRR
609     struct mtrr_sentry sentry = {
610         .base = map->base,
611         .size = map->size,
612         .type = MTRR_TYPE_UNCACHABLE
613     };
614 #endif
615
616     err = pci_device_generic_unmap_range (dev, map);
617     if (err)
618         return err;
619     
620 #ifdef HAVE_MTRR
621     if ((map->flags & PCI_DEV_MAP_FLAG_CACHABLE) != 0) {
622         sentry.type = MTRR_TYPE_WRBACK;
623     } else if ((map->flags & PCI_DEV_MAP_FLAG_WRITE_COMBINE) != 0) {
624         sentry.type = MTRR_TYPE_WRCOMB;
625     }
626
627     if (pci_sys->mtrr_fd != -1 && sentry.type != MTRR_TYPE_UNCACHABLE) {
628         if (ioctl(pci_sys->mtrr_fd, MTRRIOC_DEL_ENTRY, &sentry) < 0) {
629             /* FIXME: Should we report an error in this case?
630              */
631             fprintf(stderr, "error setting MTRR "
632                     "(base = 0x%08lx, size = 0x%08x, type = %u) %s (%d)\n",
633                     sentry.base, sentry.size, sentry.type,
634                     strerror(errno), errno);
635 /*            err = errno;*/
636         }
637     }
638 #endif
639
640     return err;
641 }
642
643 static void pci_device_linux_sysfs_enable(struct pci_device *dev)
644 {
645     char name[256];
646     int fd;
647
648     snprintf( name, 255, "%s/%04x:%02x:%02x.%1u/enable",
649               SYS_BUS_PCI,
650               dev->domain,
651               dev->bus,
652               dev->dev,
653               dev->func );
654     
655     fd = open( name, O_RDWR );
656     if (fd == -1)
657        return;
658
659     write( fd, "1", 1 );
660     close(fd);
661 }