1 /******************************************************************************
2  *
3  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
4  * Use is subject to license terms.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation;
9  * version 2.1 of the License.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "xc_private.h"
21 
22 /* Optionally flush file to disk and discard page cache */
discard_file_cache(xc_interface * xch,int fd,int flush)23 void discard_file_cache(xc_interface *xch, int fd, int flush)
24 {
25     off_t cur = 0;
26     int saved_errno = errno;
27 
28     if ( flush && (fsync(fd) < 0) )
29     {
30         /*PERROR("Failed to flush file: %s", strerror(errno));*/
31         goto out;
32     }
33 
34     /*
35      * Calculate last page boundary of amount written so far
36      * unless we are flushing in which case entire cache
37      * is discarded.
38      */
39     if ( !flush )
40     {
41         if ( (cur = lseek(fd, 0, SEEK_CUR)) == (off_t)-1 )
42             cur = 0;
43         cur &= ~(XC_PAGE_SIZE-1);
44     }
45 
46     /* Discard from the buffer cache. */
47     if ( posix_fadvise64(fd, 0, cur, POSIX_FADV_DONTNEED) < 0 )
48     {
49         /*PERROR("Failed to discard cache: %s", strerror(errno));*/
50         goto out;
51     }
52 
53  out:
54     errno = saved_errno;
55 }
56 
xc_memalign(xc_interface * xch,size_t alignment,size_t size)57 void *xc_memalign(xc_interface *xch, size_t alignment, size_t size)
58 {
59     int ret;
60     void *ptr;
61 
62     ret = posix_memalign(&ptr, alignment, size);
63     if (ret != 0 || !ptr)
64         return NULL;
65 
66     return ptr;
67 }
68 
xc_pcidev_get_gsi(xc_interface * xch,uint32_t sbdf)69 int xc_pcidev_get_gsi(xc_interface *xch, uint32_t sbdf)
70 {
71     int ret;
72     privcmd_pcidev_get_gsi_t dev_gsi = {
73         .sbdf = sbdf,
74         .gsi = 0,
75     };
76 
77     ret = ioctl(xencall_fd(xch->xcall),
78                 IOCTL_PRIVCMD_PCIDEV_GET_GSI, &dev_gsi);
79 
80     if ( ret < 0 )
81     {
82         if ( errno != ENOENT )
83             PERROR("Failed to get gsi for dev %04x:%02x:%02x.%u",
84                 sbdf >> 16, (sbdf >> 8) & 0xff, sbdf >> 3 & 0x1f, sbdf & 0x7);
85     }
86     else
87     {
88         ret = dev_gsi.gsi;
89     }
90 
91     return ret;
92 }
93 
94 /*
95  * Local variables:
96  * mode: C
97  * c-file-style: "BSD"
98  * c-basic-offset: 4
99  * tab-width: 4
100  * indent-tabs-mode: nil
101  * End:
102  */
103