9 #include <sys/syscall.h>
14 #include <xseg/xseg.h>
19 static char errbuf[ERRSIZE];
21 #define XSEG_DEVICE "/dev/xsegdev"
24 static int opendev(void)
29 fdev = open(XSEG_DEVICE, O_RDWR);
31 LOGMSG("Cannot open %s: %s\n", XSEG_DEVICE,
32 strerror_r(errno, errbuf, ERRSIZE));
38 static int closedev(void)
46 LOGMSG("Cannot close %s: %s\n", XSEG_DEVICE,
47 strerror_r(errno, errbuf, ERRSIZE));
55 static long xsegdev_allocate(const char *name, uint64_t size)
64 oldsize = ioctl(fd, XSEGDEV_IOC_SEGSIZE, 0);
66 LOGMSG("Destroying old segment\n");
67 if (ioctl(fd, XSEGDEV_IOC_DESTROYSEG, 0)) {
68 LOGMSG("Failed to destroy old segment");
74 if (ioctl(fd, XSEGDEV_IOC_CREATESEG, size)) {
75 LOGMSG("Failed to create segment");
83 static long xsegdev_deallocate(const char *name)
86 fd = open(XSEG_DEVICE, O_RDWR);
88 LOGMSG("Cannot open %s: %s\n", XSEG_DEVICE,
89 strerror_r(errno, errbuf, ERRSIZE));
93 if (ioctl(fd, XSEGDEV_IOC_DESTROYSEG, 0)) {
94 LOGMSG("Failed to destroy old segment");
102 static void *xsegdev_map(const char *name, uint64_t size)
110 xseg = mmap ( XSEG_BASE_AS_PTR,
112 PROT_READ | PROT_WRITE,
113 MAP_SHARED | MAP_FIXED /* | MAP_LOCKED */,
116 if (xseg == MAP_FAILED) {
117 LOGMSG("Could not map segment: %s\n",
118 strerror_r(errno, errbuf, ERRSIZE));
126 static void xsegdev_unmap(void *ptr, uint64_t size)
128 struct xseg *xseg = ptr;
129 (void)munmap(xseg, xseg->segment_size);
133 static struct xseg_type xseg_xsegdev = {
134 /* xseg_operations */
138 .allocate = xsegdev_allocate,
139 .deallocate = xsegdev_deallocate,
141 .unmap = xsegdev_unmap
147 static int xsegdev_signal_init(void)
152 static void xsegdev_signal_quit(void) { }
154 static int xsegdev_prepare_wait(struct xseg_port *port)
159 static int xsegdev_cancel_wait(struct xseg_port *port)
164 static int xsegdev_wait_signal(struct xseg_port *port, uint32_t timeout)
169 static int xsegdev_signal(struct xseg_port *port)
171 return write(opendev(), NULL, 0);
174 static void *xsegdev_malloc(uint64_t size)
179 static void *xsegdev_realloc(void *mem, uint64_t size)
184 static void xsegdev_mfree(void *mem) { }
186 static struct xseg_peer xseg_peer_xsegdev = {
187 /* xseg signal operations */
189 .signal_init = xsegdev_signal_init,
190 .signal_quit = xsegdev_signal_quit,
191 .prepare_wait = xsegdev_prepare_wait,
192 .cancel_wait = xsegdev_cancel_wait,
193 .wait_signal = xsegdev_wait_signal,
194 .signal = xsegdev_signal,
195 .malloc = xsegdev_malloc,
196 .realloc = xsegdev_realloc,
197 .mfree = xsegdev_mfree
203 void xseg_xsegdev_init(void)
205 xseg_register_type(&xseg_xsegdev);
206 xseg_register_peer(&xseg_peer_xsegdev);