cpu: Add HTM Instruction Flags
[gem5.git] / src / dev / virtio / fs9p.hh
1 /*
2 * Copyright (c) 2014-2017 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 #ifndef __DEV_VIRTIO_FS9P_HH__
39 #define __DEV_VIRTIO_FS9P_HH__
40
41 #include <map>
42 #include <memory>
43 #include <string>
44
45 #include "base/pollevent.hh"
46 #include "dev/virtio/base.hh"
47
48 struct VirtIO9PBaseParams;
49
50 typedef uint8_t P9MsgType;
51 typedef uint16_t P9Tag;
52
53 struct P9MsgHeader {
54 /** Length including header */
55 uint32_t len;
56 /** Message type */
57 P9MsgType type;
58 /** Message tag */
59 P9Tag tag;
60 } M5_ATTR_PACKED;
61
62 /** Convert p9 byte order (LE) to host byte order */
63 template <typename T> inline T
64 p9toh(T v) { return letoh(v); }
65
66 /** Convert host byte order to p9 byte order (LE) */
67 template <typename T> inline T
68 htop9(T v) { return htole(v); }
69
70 template <> inline P9MsgHeader
71 p9toh(P9MsgHeader v)
72 {
73 v.len = p9toh(v.len);
74 v.type = p9toh(v.type);
75 v.tag = p9toh(v.tag);
76 return v;
77 }
78
79 template <> inline P9MsgHeader
80 htop9(P9MsgHeader v)
81 {
82 v.len = htop9(v.len);
83 v.type = htop9(v.type);
84 v.tag = htop9(v.tag);
85 return v;
86 }
87
88 /**
89 * This class implements a VirtIO transport layer for the 9p network
90 * file system.
91 *
92 * The 9p VirtIO transport uses the following queues:
93 * -# 9p requests and replies
94 *
95 * Each 9p request and response is sent in its own descriptor
96 * chain. The guest initiates a transaction by packing a T message
97 * (see the 9p spec) into the first part of a descriptor chain. After
98 * the T message, the guest reserves space for the reply (R message)
99 * by including one or more writable descriptors. The server replies
100 * by writing an R message into the writable descriptors and putting
101 * the chain in the used ring (VirtQueue::produceDescriptor()).
102 *
103 * @see https://github.com/rustyrussell/virtio-spec
104 * @see https://github.com/ericvh/9p-rfc
105 * @see https://code.google.com/p/diod/wiki/protocol
106 */
107 class VirtIO9PBase : public VirtIODeviceBase
108 {
109 public:
110 typedef VirtIO9PBaseParams Params;
111 VirtIO9PBase(Params *params);
112 virtual ~VirtIO9PBase();
113
114 void readConfig(PacketPtr pkt, Addr cfgOffset);
115
116 protected:
117 /**
118 * VirtIO 9p configuration structure
119 *
120 * @note The fields in this structure depend on the features
121 * exposed to the guest.
122 */
123 struct Config {
124 uint16_t len;
125 char tag[];
126 } M5_ATTR_PACKED;
127
128 /** Currently active configuration (host byte order) */
129 std::unique_ptr<Config> config;
130
131 /** VirtIO device ID */
132 static const DeviceId ID_9P = 0x09;
133
134 /** @{
135 * @name Feature bits
136 */
137 /** Device provides a name of the resource in its configuration */
138 static const FeatureBits F_MOUNT_TAG = 0x01;
139 /** @} */
140
141 protected:
142 /**
143 * Virtqueue for 9p requests
144 */
145 class FSQueue : public VirtQueue
146 {
147 public:
148 FSQueue(PortProxy &proxy, ByteOrder bo,
149 uint16_t size, VirtIO9PBase &_parent)
150 : VirtQueue(proxy, bo, size), parent(_parent) {}
151 virtual ~FSQueue() {}
152
153 void onNotifyDescriptor(VirtDescriptor *desc);
154
155 std::string name() const { return parent.name() + ".queue"; }
156
157 protected:
158 VirtIO9PBase &parent;
159 };
160
161 FSQueue queue;
162
163 protected:
164 /**
165 * Handle incoming 9p RPC message.
166 *
167 * @param header 9p message header.
168 * @param data Pointer to data in message.
169 * @param size Size of data (excluding header)
170 */
171 virtual void recvTMsg(const P9MsgHeader &header, const uint8_t *data, size_t size) = 0;
172 /**
173 * Send a 9p RPC message reply.
174 *
175 * @param header 9p message header.
176 * @param data Pointer to data in message.
177 * @param size Size of data (excluding header)
178 */
179 void sendRMsg(const P9MsgHeader &header, const uint8_t *data, size_t size);
180
181 /**
182 * Dump a 9p RPC message on the debug output
183 *
184 * @param header 9p message header.
185 * @param data Pointer to data in message.
186 * @param size Size of data (excluding header)
187 */
188 void dumpMsg(const P9MsgHeader &header, const uint8_t *data, size_t size);
189
190 private:
191 /**
192 * Map between 9p transaction tags and descriptors where they
193 * appeared.
194 *
195 * When handling asynchronous requests, we need to ensure that
196 * replies are posted in the same descriptor as queries. The 9p
197 * RPC protocol uses the tag field in the header to match requests
198 * and replies, which we use here to find the relevant descriptor.
199 */
200 std::map<P9Tag, VirtDescriptor *> pendingTransactions;
201 };
202
203 struct VirtIO9PProxyParams;
204
205 /**
206 * VirtIO 9p proxy base class.
207 *
208 * This base class provides basic functionality shared by different 9p
209 * proxy implementations.
210 */
211 class VirtIO9PProxy : public VirtIO9PBase
212 {
213 public:
214 typedef VirtIO9PProxyParams Params;
215 VirtIO9PProxy(Params *params);
216 virtual ~VirtIO9PProxy();
217
218 void serialize(CheckpointOut &cp) const override;
219 void unserialize(CheckpointIn &cp) override;
220
221 protected:
222 void recvTMsg(const P9MsgHeader &header, const uint8_t *data,
223 size_t size) override;
224
225 /** Notification of pending data from server */
226 void serverDataReady();
227
228 /**
229 * Read data from the server behind the proxy.
230 *
231 * @note This method may return read fewer than len bytes.
232 *
233 * @param data Memory location to store results in.
234 * @param len Maximum length to read.
235 * @return Number of bytes read, -errno on failure.
236 */
237 virtual ssize_t read(uint8_t *data, size_t len) = 0;
238 /**
239 * Write data to the server behind the proxy.
240 *
241 * @note This method may return write fewer than len bytes.
242 *
243 * @param data Pointer to data to write.
244 * @param len Maximum length to write.
245 * @return Number of bytes written, -errno on failure.
246 */
247 virtual ssize_t write(const uint8_t *data, size_t len) = 0;
248
249 /**
250 * Convenience function that reads exactly len bytes.
251 *
252 * This method calls read until exactly len number of bytes has
253 * been read. A read() call is retried if the underlying syscall
254 * was interrupted.
255 *
256 * @param data Memory location to store results in.
257 * @param len Number of bytes to read.
258 */
259 void readAll(uint8_t *data, size_t len);
260 /**
261 * Convenience function that writes exactly len bytes.
262 *
263 * This method calls write until exactly len number of bytes has
264 * been written. A write() call is retried if the underlying
265 * syscall was interrupted.
266 *
267 * @param data Data to write.
268 * @param len Number of bytes to write.
269 */
270 void writeAll(const uint8_t *data, size_t len);
271
272 /**
273 * Bool to track if the device has been used or not.
274 *
275 * We need to keep track of if the device has been used as we are
276 * unable to checkpoint the device in the event that the device
277 * has been mounted in the guest system. This is due to the fact
278 * that we do not, and cannot, track the complete state across
279 * host and guest.
280 */
281 bool deviceUsed;
282 };
283
284 struct VirtIO9PDiodParams;
285
286 /**
287 * VirtIO 9p proxy that communicates with the diod 9p server using
288 * pipes.
289 */
290 class VirtIO9PDiod : public VirtIO9PProxy
291 {
292 public:
293 typedef VirtIO9PDiodParams Params;
294 VirtIO9PDiod(Params *params);
295 virtual ~VirtIO9PDiod();
296
297 void startup();
298
299 protected:
300 /**
301 * Start diod and setup the communication pipes.
302 */
303 void startDiod();
304
305 ssize_t read(uint8_t *data, size_t len);
306 ssize_t write(const uint8_t *data, size_t len);
307 /** Kill the diod child process at the end of the simulation */
308 void terminateDiod();
309
310 private:
311 class DiodDataEvent : public PollEvent
312 {
313 public:
314 DiodDataEvent(VirtIO9PDiod &_parent, int fd, int event)
315 : PollEvent(fd, event), parent(_parent) {}
316
317 virtual ~DiodDataEvent() {};
318
319 void process(int revent);
320
321 private:
322 VirtIO9PDiod &parent;
323 };
324
325 /** fd for data pipe going to diod (write end) */
326 int fd_to_diod;
327 /** fd for data pipe coming from diod (read end) */
328 int fd_from_diod;
329
330 std::unique_ptr<DiodDataEvent> dataEvent;
331
332 /** PID of diod process */
333 int diod_pid;
334 };
335
336 struct VirtIO9PSocketParams;
337
338 /**
339 * VirtIO 9p proxy that communicates with a 9p server over tcp
340 * sockets.
341 */
342 class VirtIO9PSocket : public VirtIO9PProxy
343 {
344 public:
345 typedef VirtIO9PSocketParams Params;
346 VirtIO9PSocket(Params *params);
347 virtual ~VirtIO9PSocket();
348
349 void startup();
350
351 protected:
352 /**
353 * Try to resolve the server name and connect to the 9p server.
354 */
355 void connectSocket();
356
357 /** 9p server disconnect notification */
358 void socketDisconnect();
359
360 ssize_t read(uint8_t *data, size_t len);
361 ssize_t write(const uint8_t *data, size_t len);
362
363 private:
364 class SocketDataEvent : public PollEvent
365 {
366 public:
367 SocketDataEvent(VirtIO9PSocket &_parent, int fd, int event)
368 : PollEvent(fd, event), parent(_parent) {}
369
370 virtual ~SocketDataEvent() {};
371
372 void process(int revent);
373
374 private:
375 VirtIO9PSocket &parent;
376 };
377
378 /** Socket connected to the 9p server */
379 int fdSocket;
380
381 std::unique_ptr<SocketDataEvent> dataEvent;
382 };
383
384 #endif // __DEV_VIRTIO_FS9P_HH__