aboutsummaryrefslogtreecommitdiff
path: root/libgloss/arc/hl/hl_open.c
blob: 62cbe9db46b20c1f4f0e8c7ea8aaa33a3b988a26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
 * hl_open.c -- provide _open().
 *
 * Copyright (c) 2024 Synopsys Inc.
 *
 * The authors hereby grant permission to use, copy, modify, distribute,
 * and license this software and its documentation for any purpose, provided
 * that existing copyright notices are retained in all copies and that this
 * notice is included verbatim in any distributions. No written agreement,
 * license, or royalty fee is required for any of the authorized uses.
 * Modifications to this software may be copyrighted by their authors
 * and need not follow the licensing terms described here, provided that
 * the new terms are clearly indicated on the first page of each file where
 * they apply.
 *
 */

#include <errno.h>
#include <stdarg.h>
#include <stdint.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>

#include "hl_toolchain.h"
#include "hl_api.h"

/* Map newlib open flags into Hostlink IO ones.  */
static __always_inline uint32_t
_hl_open_flags_map (int flags)
{
  uint32_t hl_flags = 0;

  hl_flags |= (flags & O_RDONLY) ? 0x0000 : 0;
  hl_flags |= (flags & O_WRONLY) ? 0x0001 : 0;
  hl_flags |= (flags & O_RDWR)   ? 0x0002 : 0;
  hl_flags |= (flags & O_APPEND) ? 0x0008 : 0;
  hl_flags |= (flags & O_CREAT)  ? 0x0100 : 0;
  hl_flags |= (flags & O_TRUNC)  ? 0x0200 : 0;
  hl_flags |= (flags & O_EXCL)   ? 0x0400 : 0;

  return hl_flags;
}

/* Open file on host.  Implements HL_SYSCALL_OPEN.  */
static __always_inline int
_hl_open (const char *path, int flags, mode_t mode)
{
  int32_t fd;
  uint32_t host_errno;
  uint32_t hl_flags = _hl_open_flags_map (flags);
  volatile __uncached char *p;

  p = _hl_message (HL_SYSCALL_OPEN, "sii:ii",
		   path,			/* s */
		   (uint32_t) hl_flags,		/* i */
		   (uint32_t) mode,		/* i */
		   (uint32_t *) &fd,		/* :i */
		   (uint32_t *) &host_errno	/* :i */);

  if (p == NULL)
    {
      errno = ETIMEDOUT;
      fd = -1;
    }
  else if (fd < 0)
    {
      errno = host_errno;
      fd = -1;
    }

  _hl_delete ();

  return fd;
}

int
_open (const char *path, int flags, ...)
{
  va_list ap;
  mode_t mode = 0;

  va_start (ap, flags);

  if (flags & O_CREAT)
    mode = va_arg (ap, mode_t);

  return _hl_open (path, flags, mode);
}