L4Re Operating System Framework – Interface and Usage Documentation
Loading...
Searching...
No Matches
vcon_stream_impl.h
1/*
2 * (c) 2010 Alexander Warg <warg@os.inf.tu-dresden.de>
3 * economic rights: Technische Universität Dresden (Germany)
4 *
5 * This file is part of TUD:OS and distributed under the terms of the
6 * GNU General Public License 2.
7 * Please see the COPYING-GPL-2 file for details.
8 *
9 * As a special exception, you may use this file as part of a free software
10 * library without restriction. Specifically, if other files instantiate
11 * templates or use macros or inline functions from this file, or you compile
12 * this file and link it with other files to produce an executable, this
13 * file does not by itself cause the resulting executable to be covered by
14 * the GNU General Public License. This exception does not however
15 * invalidate any other reasons why the executable file might be covered by
16 * the GNU General Public License.
17 */
18
19#include <l4/re/env>
20#include <l4/sys/factory>
21
22#include "vcon_stream.h"
23
24#include <termios.h>
25#include <unistd.h>
26#include <sys/ioctl.h>
27#include <sys/ttydefaults.h>
28
29namespace L4Re { namespace Core {
30Vcon_stream::Vcon_stream(L4::Cap<L4::Vcon> s) noexcept
31: Be_file_stream(),
32 _s(s), _irq(L4Re::virt_cap_alloc->alloc<L4::Semaphore>()), _irq_bound(false)
33{
34 int res = l4_error(L4Re::Env::env()->factory()->create(_irq));
35 (void)res; // handle errors!
36}
37
38ssize_t
39Vcon_stream::readv(const struct iovec *iovec, int iovcnt) noexcept
40{
41 if (!_irq_bound)
42 {
43 bool was_bound = __atomic_exchange_n(&_irq_bound, true, __ATOMIC_SEQ_CST);
44 if (!was_bound)
45 if (l4_error(_s->bind(0, _irq)) < 0)
46 return -EIO;
47 }
48
49 ssize_t bytes = 0;
50 for (; iovcnt > 0; --iovcnt, ++iovec)
51 {
52 if (iovec->iov_len == 0)
53 continue;
54
55 char *buf = (char *)iovec->iov_base;
56 size_t len = iovec->iov_len;
57
58 while (1)
59 {
60 int ret = _s->read(buf, len);
61
62 // BS: what is this ??
63 if (ret > (int)len)
64 ret = len;
65
66 if (ret < 0)
67 return ret;
68 else if (ret == 0)
69 {
70 if (bytes)
71 return bytes;
72
73 ret = _s->read(buf, len);
74 if (ret < 0)
75 return ret;
76 else if (ret == 0)
77 {
78 _irq->down();
79 continue;
80 }
81 }
82
83 bytes += ret;
84 len -= ret;
85 buf += ret;
86
87 if (len == 0)
88 break;
89 }
90 }
91
92 return bytes;
93}
94
95ssize_t
96Vcon_stream::writev(const struct iovec *iovec, int iovcnt) noexcept
97{
98 l4_msg_regs_t store;
100
101 Vfs_config::memcpy(&store, mr, sizeof(store));
102
103 ssize_t written = 0;
104 while (iovcnt)
105 {
106 size_t sl = iovec->iov_len;
107 char const *b = (char const *)iovec->iov_base;
108
109 for (; sl > L4_VCON_WRITE_SIZE
111 _s->send(b, L4_VCON_WRITE_SIZE);
112
113 _s->send(b, sl);
114
115 written += iovec->iov_len;
116
117 ++iovec;
118 --iovcnt;
119 }
120 Vfs_config::memcpy(mr, &store, sizeof(store));
121 return written;
122}
123
124int
125Vcon_stream::fstat64(struct stat64 *buf) const noexcept
126{
127 buf->st_size = 0;
128 buf->st_mode = 0666;
129 buf->st_dev = _s.cap();
130 buf->st_ino = 0;
131 return 0;
132}
133
134int
135Vcon_stream::ioctl(unsigned long request, va_list args) noexcept
136{
137 switch (request) {
138 case TCGETS:
139 {
140 //vt100_tcgetattr(term, (struct termios *)argp);
141
142 struct termios *t = va_arg(args, struct termios *);
143
144 l4_vcon_attr_t l4a;
145 if (!l4_error(_s->get_attr(&l4a)))
146 {
147 t->c_iflag = l4a.i_flags;
148 t->c_oflag = l4a.o_flags; // output flags
149 t->c_cflag = 0; // control flags
150 t->c_lflag = l4a.l_flags; // local flags
151 }
152 else
153 t->c_iflag = t->c_oflag = t->c_cflag = t->c_lflag = 0;
154#if 0
155 //t->c_lflag |= ECHO; // if term->echo
156 t->c_lflag |= ICANON; // if term->term_mode == VT100MODE_COOKED
157#endif
158
159 t->c_cc[VEOF] = CEOF;
160 t->c_cc[VEOL] = _POSIX_VDISABLE;
161 t->c_cc[VEOL2] = _POSIX_VDISABLE;
162 t->c_cc[VERASE] = CERASE;
163 t->c_cc[VWERASE]= CWERASE;
164 t->c_cc[VKILL] = CKILL;
165 t->c_cc[VREPRINT]=CREPRINT;
166 t->c_cc[VINTR] = CINTR;
167 t->c_cc[VQUIT] = _POSIX_VDISABLE;
168 t->c_cc[VSUSP] = CSUSP;
169 t->c_cc[VSTART] = CSTART;
170 t->c_cc[VSTOP] = CSTOP;
171 t->c_cc[VLNEXT] = CLNEXT;
172 t->c_cc[VDISCARD]=CDISCARD;
173 t->c_cc[VMIN] = CMIN;
174 t->c_cc[VTIME] = 0;
175
176 }
177
178 return 0;
179
180 case TCSETS:
181 case TCSETSW:
182 case TCSETSF:
183 {
184 //vt100_tcsetattr(term, (struct termios *)argp);
185 struct termios const *t = va_arg(args, struct termios const *);
186
187 // XXX: well, we're cheating, get this from the other side!
188
189 l4_vcon_attr_t l4a;
190 l4a.i_flags = t->c_iflag;
191 l4a.o_flags = t->c_oflag; // output flags
192 l4a.l_flags = t->c_lflag; // local flags
193 _s->set_attr(&l4a);
194 }
195 return 0;
196
197 default:
198 break;
199 };
200 return -EINVAL;
201}
202
203}}
static Env const * env() noexcept
Returns the initial environment for the current task.
Definition env:103
C++ interface for capabilities.
Definition capability.h:222
Environment interface.
Common factory related definitions.
long l4_error(l4_msgtag_t tag) L4_NOTHROW
Get IPC error code if any or message tag label otherwise for an IPC call.
Definition ipc.h:636
l4_msg_regs_t * l4_utcb_mr(void) L4_NOTHROW L4_PURE
Get the message-register block of a UTCB.
Definition utcb.h:352
@ L4_VCON_WRITE_SIZE
Maximum size that can be written with one l4_vcon_write call.
Definition vcon.h:109
L4Re C++ Interfaces.
Definition cmd_control:15
Vcon attribute structure.
Definition vcon.h:197
l4_umword_t i_flags
input flags
Definition vcon.h:198
l4_umword_t o_flags
output flags
Definition vcon.h:199
l4_umword_t l_flags
local flags
Definition vcon.h:200
Encapsulation of the message-register block in the UTCB.
Definition utcb.h:79