]>
Commit | Line | Data |
---|---|---|
6d533105 VI |
1 | /* |
2 | * hl_write.c -- provide _write(). | |
3 | * | |
4 | * Copyright (c) 2024 Synopsys Inc. | |
5 | * | |
6 | * The authors hereby grant permission to use, copy, modify, distribute, | |
7 | * and license this software and its documentation for any purpose, provided | |
8 | * that existing copyright notices are retained in all copies and that this | |
9 | * notice is included verbatim in any distributions. No written agreement, | |
10 | * license, or royalty fee is required for any of the authorized uses. | |
11 | * Modifications to this software may be copyrighted by their authors | |
12 | * and need not follow the licensing terms described here, provided that | |
13 | * the new terms are clearly indicated on the first page of each file where | |
14 | * they apply. | |
15 | * | |
16 | */ | |
17 | ||
18 | #include <errno.h> | |
19 | #include <stdint.h> | |
20 | #include <stddef.h> | |
21 | #include <unistd.h> | |
22 | ||
23 | #include "hl_toolchain.h" | |
24 | #include "hl_api.h" | |
25 | ||
26 | ||
27 | /* Write one chunk to the host file. Implements HL_SYSCALL_WRITE. */ | |
28 | static ssize_t | |
29 | _hl_write (int fd, const char *buf, size_t nbyte) | |
30 | { | |
31 | ssize_t ret; | |
32 | int32_t n; | |
33 | uint32_t host_errno; | |
34 | volatile __uncached char *p; | |
35 | ||
36 | p = _hl_message (HL_SYSCALL_WRITE, "ipi:ii", | |
37 | (uint32_t) fd, /* i */ | |
38 | buf, (uint32_t) nbyte, /* p */ | |
39 | (uint32_t) nbyte, /* i */ | |
40 | (uint32_t *) &n, /* :i */ | |
41 | (uint32_t *) &host_errno /* :i */); | |
42 | ||
43 | if (p == NULL) | |
44 | { | |
45 | errno = ETIMEDOUT; | |
46 | ret = -1; | |
47 | } | |
48 | else if (n < 0) | |
49 | { | |
50 | errno = host_errno; | |
51 | ret = -1; | |
52 | } | |
53 | else | |
54 | { | |
55 | ret = n; | |
56 | } | |
57 | ||
58 | _hl_delete (); | |
59 | ||
60 | return ret; | |
61 | } | |
62 | ||
63 | ssize_t | |
64 | _write (int fd, const char *buf, size_t nbyte) | |
65 | { | |
66 | const uint32_t hl_iochunk = _hl_iochunk_size (); | |
67 | size_t to_write = nbyte; | |
68 | size_t offset = 0; | |
69 | ssize_t ret = 0; | |
70 | ||
71 | while (to_write > hl_iochunk) | |
72 | { | |
73 | ret = _hl_write (fd, buf + offset, hl_iochunk); | |
74 | ||
75 | if (ret < 0) | |
76 | return ret; | |
77 | ||
78 | offset += ret; | |
79 | ||
80 | if (ret != (ssize_t) hl_iochunk) | |
81 | return offset; | |
82 | ||
83 | to_write -= hl_iochunk; | |
84 | } | |
85 | ||
86 | if (to_write) | |
87 | { | |
88 | ret = _hl_write (fd, buf + offset, to_write); | |
89 | ||
90 | if (ret < 0) | |
91 | return ret; | |
92 | ||
93 | ret += offset; | |
94 | } | |
95 | ||
96 | return ret; | |
97 | } |