4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <sys/ioctl.h>
40 #include <rte_memory.h>
41 #include <rte_eal_memconfig.h>
44 #include "eal_filesystem.h"
46 #include "eal_private.h"
50 /* per-process VFIO config */
51 static struct vfio_config vfio_cfg;
53 static int vfio_type1_dma_map(int);
54 static int vfio_spapr_dma_map(int);
55 static int vfio_noiommu_dma_map(int);
57 /* IOMMU types we support */
58 static const struct vfio_iommu_type iommu_types[] = {
59 /* x86 IOMMU, otherwise known as type 1 */
60 { RTE_VFIO_TYPE1, "Type 1", &vfio_type1_dma_map},
61 /* ppc64 IOMMU, otherwise known as spapr */
62 { RTE_VFIO_SPAPR, "sPAPR", &vfio_spapr_dma_map},
64 { RTE_VFIO_NOIOMMU, "No-IOMMU", &vfio_noiommu_dma_map},
68 vfio_get_group_fd(int iommu_group_no)
72 char filename[PATH_MAX];
73 struct vfio_group *cur_grp;
75 /* check if we already have the group descriptor open */
76 for (i = 0; i < VFIO_MAX_GROUPS; i++)
77 if (vfio_cfg.vfio_groups[i].group_no == iommu_group_no)
78 return vfio_cfg.vfio_groups[i].fd;
80 /* Lets see first if there is room for a new group */
81 if (vfio_cfg.vfio_active_groups == VFIO_MAX_GROUPS) {
82 RTE_LOG(ERR, EAL, "Maximum number of VFIO groups reached!\n");
86 /* Now lets get an index for the new group */
87 for (i = 0; i < VFIO_MAX_GROUPS; i++)
88 if (vfio_cfg.vfio_groups[i].group_no == -1) {
89 cur_grp = &vfio_cfg.vfio_groups[i];
93 /* This should not happen */
94 if (i == VFIO_MAX_GROUPS) {
95 RTE_LOG(ERR, EAL, "No VFIO group free slot found\n");
98 /* if primary, try to open the group */
99 if (internal_config.process_type == RTE_PROC_PRIMARY) {
100 /* try regular group format */
101 snprintf(filename, sizeof(filename),
102 VFIO_GROUP_FMT, iommu_group_no);
103 vfio_group_fd = open(filename, O_RDWR);
104 if (vfio_group_fd < 0) {
105 /* if file not found, it's not an error */
106 if (errno != ENOENT) {
107 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
112 /* special case: try no-IOMMU path as well */
113 snprintf(filename, sizeof(filename),
114 VFIO_NOIOMMU_GROUP_FMT, iommu_group_no);
115 vfio_group_fd = open(filename, O_RDWR);
116 if (vfio_group_fd < 0) {
117 if (errno != ENOENT) {
118 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", filename,
124 /* noiommu group found */
127 cur_grp->group_no = iommu_group_no;
128 cur_grp->fd = vfio_group_fd;
129 vfio_cfg.vfio_active_groups++;
130 return vfio_group_fd;
132 /* if we're in a secondary process, request group fd from the primary
133 * process via our socket
138 socket_fd = vfio_mp_sync_connect_to_primary();
141 RTE_LOG(ERR, EAL, " cannot connect to primary process!\n");
144 if (vfio_mp_sync_send_request(socket_fd, SOCKET_REQ_GROUP) < 0) {
145 RTE_LOG(ERR, EAL, " cannot request container fd!\n");
149 if (vfio_mp_sync_send_request(socket_fd, iommu_group_no) < 0) {
150 RTE_LOG(ERR, EAL, " cannot send group number!\n");
154 ret = vfio_mp_sync_receive_request(socket_fd);
160 vfio_group_fd = vfio_mp_sync_receive_fd(socket_fd);
161 /* if we got the fd, store it and return it */
162 if (vfio_group_fd > 0) {
164 cur_grp->group_no = iommu_group_no;
165 cur_grp->fd = vfio_group_fd;
166 vfio_cfg.vfio_active_groups++;
167 return vfio_group_fd;
169 /* fall-through on error */
171 RTE_LOG(ERR, EAL, " cannot get container fd!\n");
181 get_vfio_group_idx(int vfio_group_fd)
184 for (i = 0; i < VFIO_MAX_GROUPS; i++)
185 if (vfio_cfg.vfio_groups[i].fd == vfio_group_fd)
191 vfio_group_device_get(int vfio_group_fd)
195 i = get_vfio_group_idx(vfio_group_fd);
196 if (i < 0 || i > (VFIO_MAX_GROUPS - 1))
197 RTE_LOG(ERR, EAL, " wrong vfio_group index (%d)\n", i);
199 vfio_cfg.vfio_groups[i].devices++;
203 vfio_group_device_put(int vfio_group_fd)
207 i = get_vfio_group_idx(vfio_group_fd);
208 if (i < 0 || i > (VFIO_MAX_GROUPS - 1))
209 RTE_LOG(ERR, EAL, " wrong vfio_group index (%d)\n", i);
211 vfio_cfg.vfio_groups[i].devices--;
215 vfio_group_device_count(int vfio_group_fd)
219 i = get_vfio_group_idx(vfio_group_fd);
220 if (i < 0 || i > (VFIO_MAX_GROUPS - 1)) {
221 RTE_LOG(ERR, EAL, " wrong vfio_group index (%d)\n", i);
225 return vfio_cfg.vfio_groups[i].devices;
229 clear_group(int vfio_group_fd)
234 if (internal_config.process_type == RTE_PROC_PRIMARY) {
236 i = get_vfio_group_idx(vfio_group_fd);
239 vfio_cfg.vfio_groups[i].group_no = -1;
240 vfio_cfg.vfio_groups[i].fd = -1;
241 vfio_cfg.vfio_groups[i].devices = 0;
242 vfio_cfg.vfio_active_groups--;
246 /* This is just for SECONDARY processes */
247 socket_fd = vfio_mp_sync_connect_to_primary();
250 RTE_LOG(ERR, EAL, " cannot connect to primary process!\n");
254 if (vfio_mp_sync_send_request(socket_fd, SOCKET_CLR_GROUP) < 0) {
255 RTE_LOG(ERR, EAL, " cannot request container fd!\n");
260 if (vfio_mp_sync_send_request(socket_fd, vfio_group_fd) < 0) {
261 RTE_LOG(ERR, EAL, " cannot send group fd!\n");
266 ret = vfio_mp_sync_receive_request(socket_fd);
269 RTE_LOG(ERR, EAL, " BAD VFIO group fd!\n");
276 RTE_LOG(ERR, EAL, " Socket error\n");
280 RTE_LOG(ERR, EAL, " UNKNOWN reply, %d\n", ret);
287 rte_vfio_setup_device(const char *sysfs_base, const char *dev_addr,
288 int *vfio_dev_fd, struct vfio_device_info *device_info)
290 struct vfio_group_status group_status = {
291 .argsz = sizeof(group_status)
297 /* get group number */
298 ret = vfio_get_group_no(sysfs_base, dev_addr, &iommu_group_no);
300 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
305 /* if negative, something failed */
309 /* get the actual group fd */
310 vfio_group_fd = vfio_get_group_fd(iommu_group_no);
311 if (vfio_group_fd < 0)
314 /* if group_fd == 0, that means the device isn't managed by VFIO */
315 if (vfio_group_fd == 0) {
316 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
322 * at this point, we know that this group is viable (meaning, all devices
323 * are either bound to VFIO or not bound to anything)
326 /* check if the group is viable */
327 ret = ioctl(vfio_group_fd, VFIO_GROUP_GET_STATUS, &group_status);
329 RTE_LOG(ERR, EAL, " %s cannot get group status, "
330 "error %i (%s)\n", dev_addr, errno, strerror(errno));
331 close(vfio_group_fd);
332 clear_group(vfio_group_fd);
334 } else if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE)) {
335 RTE_LOG(ERR, EAL, " %s VFIO group is not viable!\n", dev_addr);
336 close(vfio_group_fd);
337 clear_group(vfio_group_fd);
341 /* check if group does not have a container yet */
342 if (!(group_status.flags & VFIO_GROUP_FLAGS_CONTAINER_SET)) {
344 /* add group to a container */
345 ret = ioctl(vfio_group_fd, VFIO_GROUP_SET_CONTAINER,
346 &vfio_cfg.vfio_container_fd);
348 RTE_LOG(ERR, EAL, " %s cannot add VFIO group to container, "
349 "error %i (%s)\n", dev_addr, errno, strerror(errno));
350 close(vfio_group_fd);
351 clear_group(vfio_group_fd);
356 * pick an IOMMU type and set up DMA mappings for container
358 * needs to be done only once, only when first group is
359 * assigned to a container and only in primary process.
360 * Note this can happen several times with the hotplug
363 if (internal_config.process_type == RTE_PROC_PRIMARY &&
364 vfio_cfg.vfio_active_groups == 1) {
365 /* select an IOMMU type which we will be using */
366 const struct vfio_iommu_type *t =
367 vfio_set_iommu_type(vfio_cfg.vfio_container_fd);
370 " %s failed to select IOMMU type\n",
372 close(vfio_group_fd);
373 clear_group(vfio_group_fd);
376 ret = t->dma_map_func(vfio_cfg.vfio_container_fd);
379 " %s DMA remapping failed, error %i (%s)\n",
380 dev_addr, errno, strerror(errno));
381 close(vfio_group_fd);
382 clear_group(vfio_group_fd);
388 /* get a file descriptor for the device */
389 *vfio_dev_fd = ioctl(vfio_group_fd, VFIO_GROUP_GET_DEVICE_FD, dev_addr);
390 if (*vfio_dev_fd < 0) {
391 /* if we cannot get a device fd, this implies a problem with
392 * the VFIO group or the container not having IOMMU configured.
395 RTE_LOG(WARNING, EAL, "Getting a vfio_dev_fd for %s failed\n",
397 close(vfio_group_fd);
398 clear_group(vfio_group_fd);
402 /* test and setup the device */
403 ret = ioctl(*vfio_dev_fd, VFIO_DEVICE_GET_INFO, device_info);
405 RTE_LOG(ERR, EAL, " %s cannot get device info, "
406 "error %i (%s)\n", dev_addr, errno,
409 close(vfio_group_fd);
410 clear_group(vfio_group_fd);
413 vfio_group_device_get(vfio_group_fd);
419 rte_vfio_release_device(const char *sysfs_base, const char *dev_addr,
422 struct vfio_group_status group_status = {
423 .argsz = sizeof(group_status)
429 /* get group number */
430 ret = vfio_get_group_no(sysfs_base, dev_addr, &iommu_group_no);
432 RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver\n",
434 /* This is an error at this point. */
438 /* get the actual group fd */
439 vfio_group_fd = vfio_get_group_fd(iommu_group_no);
440 if (vfio_group_fd <= 0) {
441 RTE_LOG(INFO, EAL, "vfio_get_group_fd failed for %s\n",
446 /* At this point we got an active group. Closing it will make the
447 * container detachment. If this is the last active group, VFIO kernel
448 * code will unset the container and the IOMMU mappings.
451 /* Closing a device */
452 if (close(vfio_dev_fd) < 0) {
453 RTE_LOG(INFO, EAL, "Error when closing vfio_dev_fd for %s\n",
458 /* An VFIO group can have several devices attached. Just when there is
459 * no devices remaining should the group be closed.
461 vfio_group_device_put(vfio_group_fd);
462 if (!vfio_group_device_count(vfio_group_fd)) {
464 if (close(vfio_group_fd) < 0) {
465 RTE_LOG(INFO, EAL, "Error when closing vfio_group_fd for %s\n",
470 if (clear_group(vfio_group_fd) < 0) {
471 RTE_LOG(INFO, EAL, "Error when clearing group for %s\n",
481 rte_vfio_enable(const char *modname)
483 /* initialize group list */
487 for (i = 0; i < VFIO_MAX_GROUPS; i++) {
488 vfio_cfg.vfio_groups[i].fd = -1;
489 vfio_cfg.vfio_groups[i].group_no = -1;
490 vfio_cfg.vfio_groups[i].devices = 0;
493 /* inform the user that we are probing for VFIO */
494 RTE_LOG(INFO, EAL, "Probing VFIO support...\n");
496 /* check if vfio module is loaded */
497 vfio_available = rte_eal_check_module(modname);
499 /* return error directly */
500 if (vfio_available == -1) {
501 RTE_LOG(INFO, EAL, "Could not get loaded module details!\n");
505 /* return 0 if VFIO modules not loaded */
506 if (vfio_available == 0) {
507 RTE_LOG(DEBUG, EAL, "VFIO modules not loaded, "
508 "skipping VFIO support...\n");
512 vfio_cfg.vfio_container_fd = vfio_get_container_fd();
514 /* check if we have VFIO driver enabled */
515 if (vfio_cfg.vfio_container_fd != -1) {
516 RTE_LOG(NOTICE, EAL, "VFIO support initialized\n");
517 vfio_cfg.vfio_enabled = 1;
519 RTE_LOG(NOTICE, EAL, "VFIO support could not be initialized\n");
526 rte_vfio_is_enabled(const char *modname)
528 const int mod_available = rte_eal_check_module(modname);
529 return vfio_cfg.vfio_enabled && mod_available;
532 const struct vfio_iommu_type *
533 vfio_set_iommu_type(int vfio_container_fd)
536 for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
537 const struct vfio_iommu_type *t = &iommu_types[idx];
539 int ret = ioctl(vfio_container_fd, VFIO_SET_IOMMU,
542 RTE_LOG(NOTICE, EAL, " using IOMMU type %d (%s)\n",
543 t->type_id, t->name);
546 /* not an error, there may be more supported IOMMU types */
547 RTE_LOG(DEBUG, EAL, " set IOMMU type %d (%s) failed, "
548 "error %i (%s)\n", t->type_id, t->name, errno,
551 /* if we didn't find a suitable IOMMU type, fail */
556 vfio_has_supported_extensions(int vfio_container_fd)
559 unsigned idx, n_extensions = 0;
560 for (idx = 0; idx < RTE_DIM(iommu_types); idx++) {
561 const struct vfio_iommu_type *t = &iommu_types[idx];
563 ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION,
566 RTE_LOG(ERR, EAL, " could not get IOMMU type, "
567 "error %i (%s)\n", errno,
569 close(vfio_container_fd);
571 } else if (ret == 1) {
572 /* we found a supported extension */
575 RTE_LOG(DEBUG, EAL, " IOMMU type %d (%s) is %s\n",
577 ret ? "supported" : "not supported");
580 /* if we didn't find any supported IOMMU types, fail */
582 close(vfio_container_fd);
590 vfio_get_container_fd(void)
592 int ret, vfio_container_fd;
594 /* if we're in a primary process, try to open the container */
595 if (internal_config.process_type == RTE_PROC_PRIMARY) {
596 vfio_container_fd = open(VFIO_CONTAINER_PATH, O_RDWR);
597 if (vfio_container_fd < 0) {
598 RTE_LOG(ERR, EAL, " cannot open VFIO container, "
599 "error %i (%s)\n", errno, strerror(errno));
603 /* check VFIO API version */
604 ret = ioctl(vfio_container_fd, VFIO_GET_API_VERSION);
605 if (ret != VFIO_API_VERSION) {
607 RTE_LOG(ERR, EAL, " could not get VFIO API version, "
608 "error %i (%s)\n", errno, strerror(errno));
610 RTE_LOG(ERR, EAL, " unsupported VFIO API version!\n");
611 close(vfio_container_fd);
615 ret = vfio_has_supported_extensions(vfio_container_fd);
617 RTE_LOG(ERR, EAL, " no supported IOMMU "
618 "extensions found!\n");
622 return vfio_container_fd;
625 * if we're in a secondary process, request container fd from the
626 * primary process via our socket
630 socket_fd = vfio_mp_sync_connect_to_primary();
632 RTE_LOG(ERR, EAL, " cannot connect to primary process!\n");
635 if (vfio_mp_sync_send_request(socket_fd, SOCKET_REQ_CONTAINER) < 0) {
636 RTE_LOG(ERR, EAL, " cannot request container fd!\n");
640 vfio_container_fd = vfio_mp_sync_receive_fd(socket_fd);
641 if (vfio_container_fd < 0) {
642 RTE_LOG(ERR, EAL, " cannot get container fd!\n");
647 return vfio_container_fd;
654 vfio_get_group_no(const char *sysfs_base,
655 const char *dev_addr, int *iommu_group_no)
657 char linkname[PATH_MAX];
658 char filename[PATH_MAX];
659 char *tok[16], *group_tok, *end;
662 memset(linkname, 0, sizeof(linkname));
663 memset(filename, 0, sizeof(filename));
665 /* try to find out IOMMU group for this device */
666 snprintf(linkname, sizeof(linkname),
667 "%s/%s/iommu_group", sysfs_base, dev_addr);
669 ret = readlink(linkname, filename, sizeof(filename));
671 /* if the link doesn't exist, no VFIO for us */
675 ret = rte_strsplit(filename, sizeof(filename),
676 tok, RTE_DIM(tok), '/');
679 RTE_LOG(ERR, EAL, " %s cannot get IOMMU group\n", dev_addr);
683 /* IOMMU group is always the last token */
685 group_tok = tok[ret - 1];
687 *iommu_group_no = strtol(group_tok, &end, 10);
688 if ((end != group_tok && *end != '\0') || errno != 0) {
689 RTE_LOG(ERR, EAL, " %s error parsing IOMMU number!\n", dev_addr);
697 vfio_type1_dma_map(int vfio_container_fd)
699 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
702 /* map all DPDK segments for DMA. use 1:1 PA to IOVA mapping */
703 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
704 struct vfio_iommu_type1_dma_map dma_map;
706 if (ms[i].addr == NULL)
709 memset(&dma_map, 0, sizeof(dma_map));
710 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
711 dma_map.vaddr = ms[i].addr_64;
712 dma_map.size = ms[i].len;
713 if (rte_eal_iova_mode() == RTE_IOVA_VA)
714 dma_map.iova = dma_map.vaddr;
716 dma_map.iova = ms[i].iova;
717 dma_map.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE;
719 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
722 RTE_LOG(ERR, EAL, " cannot set up DMA remapping, "
723 "error %i (%s)\n", errno,
733 vfio_spapr_dma_map(int vfio_container_fd)
735 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
738 struct vfio_iommu_spapr_register_memory reg = {
739 .argsz = sizeof(reg),
742 struct vfio_iommu_spapr_tce_info info = {
743 .argsz = sizeof(info),
745 struct vfio_iommu_spapr_tce_create create = {
746 .argsz = sizeof(create),
748 struct vfio_iommu_spapr_tce_remove remove = {
749 .argsz = sizeof(remove),
752 /* query spapr iommu info */
753 ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_GET_INFO, &info);
755 RTE_LOG(ERR, EAL, " cannot get iommu info, "
756 "error %i (%s)\n", errno, strerror(errno));
760 /* remove default DMA of 32 bit window */
761 remove.start_addr = info.dma32_window_start;
762 ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_REMOVE, &remove);
764 RTE_LOG(ERR, EAL, " cannot remove default DMA window, "
765 "error %i (%s)\n", errno, strerror(errno));
769 /* create DMA window from 0 to max(phys_addr + len) */
770 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
771 if (ms[i].addr == NULL)
774 create.window_size = RTE_MAX(create.window_size,
775 ms[i].iova + ms[i].len);
778 /* sPAPR requires window size to be a power of 2 */
779 create.window_size = rte_align64pow2(create.window_size);
780 create.page_shift = __builtin_ctzll(ms->hugepage_sz);
783 ret = ioctl(vfio_container_fd, VFIO_IOMMU_SPAPR_TCE_CREATE, &create);
785 RTE_LOG(ERR, EAL, " cannot create new DMA window, "
786 "error %i (%s)\n", errno, strerror(errno));
790 if (create.start_addr != 0) {
791 RTE_LOG(ERR, EAL, " DMA window start address != 0\n");
795 /* map all DPDK segments for DMA. use 1:1 PA to IOVA mapping */
796 for (i = 0; i < RTE_MAX_MEMSEG; i++) {
797 struct vfio_iommu_type1_dma_map dma_map;
799 if (ms[i].addr == NULL)
802 reg.vaddr = (uintptr_t) ms[i].addr;
803 reg.size = ms[i].len;
804 ret = ioctl(vfio_container_fd,
805 VFIO_IOMMU_SPAPR_REGISTER_MEMORY, ®);
807 RTE_LOG(ERR, EAL, " cannot register vaddr for IOMMU, "
808 "error %i (%s)\n", errno, strerror(errno));
812 memset(&dma_map, 0, sizeof(dma_map));
813 dma_map.argsz = sizeof(struct vfio_iommu_type1_dma_map);
814 dma_map.vaddr = ms[i].addr_64;
815 dma_map.size = ms[i].len;
816 if (rte_eal_iova_mode() == RTE_IOVA_VA)
817 dma_map.iova = dma_map.vaddr;
819 dma_map.iova = ms[i].iova;
820 dma_map.flags = VFIO_DMA_MAP_FLAG_READ |
821 VFIO_DMA_MAP_FLAG_WRITE;
823 ret = ioctl(vfio_container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
826 RTE_LOG(ERR, EAL, " cannot set up DMA remapping, "
827 "error %i (%s)\n", errno, strerror(errno));
837 vfio_noiommu_dma_map(int __rte_unused vfio_container_fd)
839 /* No-IOMMU mode does not need DMA mapping */
844 rte_vfio_noiommu_is_enabled(void)
846 int fd, ret, cnt __rte_unused;
850 fd = open(VFIO_NOIOMMU_MODE, O_RDONLY);
854 cnt = read(fd, &c, 1);