virtio_console.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. "use strict";
  2. // https://docs.oasis-open.org/virtio/virtio/v1.2/csd01/virtio-v1.2-csd01.html#x1-2900003
  3. const VIRTIO_CONSOLE_DEVICE_READY = 0;
  4. const VIRTIO_CONSOLE_DEVICE_ADD = 1;
  5. const VIRTIO_CONSOLE_DEVICE_REMOVE = 2;
  6. const VIRTIO_CONSOLE_PORT_READY = 3;
  7. const VIRTIO_CONSOLE_CONSOLE_PORT = 4;
  8. const VIRTIO_CONSOLE_RESIZE = 5;
  9. const VIRTIO_CONSOLE_PORT_OPEN = 6;
  10. const VIRTIO_CONSOLE_PORT_NAME = 7;
  11. const VIRTIO_CONSOLE_F_SIZE = 0;
  12. const VIRTIO_CONSOLE_F_MULTIPORT = 1;
  13. const VIRTIO_CONSOLE_F_EMERG_WRITE = 2;
  14. /**
  15. * @constructor
  16. *
  17. * @param {CPU} cpu
  18. */
  19. function VirtioConsole(cpu, bus)
  20. {
  21. /** @const @type {BusConnector} */
  22. this.bus = bus;
  23. this.rows = 25;
  24. this.cols = 80;
  25. this.ports = 4;
  26. let queues = [
  27. {
  28. size_supported: 16,
  29. notify_offset: 0,
  30. },
  31. {
  32. size_supported: 16,
  33. notify_offset: 1,
  34. },
  35. {
  36. size_supported: 16,
  37. notify_offset: 2,
  38. },
  39. {
  40. size_supported: 16,
  41. notify_offset: 3,
  42. },
  43. ];
  44. for (let i = 1; i < this.ports; ++i)
  45. {
  46. queues.push({size_supported: 16, notify_offset: 0});
  47. queues.push({size_supported: 8, notify_offset: 1});
  48. }
  49. /** @type {VirtIO} */
  50. this.virtio = new VirtIO(cpu,
  51. {
  52. name: "virtio-console",
  53. pci_id: 0x07 << 3,
  54. device_id: 0x1043,
  55. subsystem_device_id: 3,
  56. common:
  57. {
  58. initial_port: 0xB800,
  59. queues: queues,
  60. features:
  61. [
  62. VIRTIO_CONSOLE_F_SIZE,
  63. VIRTIO_CONSOLE_F_MULTIPORT,
  64. VIRTIO_F_VERSION_1,
  65. ],
  66. on_driver_ok: () => {},
  67. },
  68. notification:
  69. {
  70. initial_port: 0xB900,
  71. single_handler: false,
  72. handlers:
  73. [
  74. (queue_id) =>
  75. {
  76. let queue = this.virtio.queues[queue_id];
  77. // TODO: Full buffer looks like an empty buffer so prevent it from filling
  78. while (queue.count_requests() > queue.size - 2) queue.pop_request();
  79. },
  80. (queue_id) =>
  81. {
  82. let queue = this.virtio.queues[queue_id];
  83. let port = queue_id > 3 ? (queue_id-3 >> 1) : 0;
  84. while(queue.has_request())
  85. {
  86. const bufchain = queue.pop_request();
  87. const buffer = new Uint8Array(bufchain.length_readable);
  88. bufchain.get_next_blob(buffer);
  89. this.bus.send("virtio-console" + port + "-output-bytes", buffer);
  90. this.Ack(queue_id, bufchain);
  91. }
  92. },
  93. (queue_id) =>
  94. {
  95. if(queue_id != 2)
  96. {
  97. dbg_assert(false, "VirtioConsole Notified for wrong queue: " + queue_id +
  98. " (expected queue_id of 2)");
  99. return;
  100. }
  101. let queue = this.virtio.queues[queue_id];
  102. // Full buffer looks like an empty buffer so prevent it from filling
  103. while (queue.count_requests() > queue.size - 2) queue.pop_request();
  104. },
  105. (queue_id) =>
  106. {
  107. if(queue_id != 3)
  108. {
  109. dbg_assert(false, "VirtioConsole Notified for wrong queue: " + queue_id +
  110. " (expected queue_id of 3)");
  111. return;
  112. }
  113. let queue = this.virtio.queues[queue_id];
  114. while(queue.has_request())
  115. {
  116. const bufchain = queue.pop_request();
  117. const buffer = new Uint8Array(bufchain.length_readable);
  118. bufchain.get_next_blob(buffer);
  119. let parts = marshall.Unmarshall(["w", "h", "h"], buffer, { offset : 0 });
  120. let port = parts[0];
  121. let event = parts[1];
  122. let value = parts[2];
  123. this.Ack(queue_id, bufchain);
  124. switch(event) {
  125. case VIRTIO_CONSOLE_DEVICE_READY:
  126. for (let i = 0; i < this.ports; ++i) {
  127. this.SendEvent(i, VIRTIO_CONSOLE_DEVICE_ADD, 0);
  128. }
  129. break;
  130. case VIRTIO_CONSOLE_PORT_READY:
  131. this.Ack(queue_id, bufchain);
  132. this.SendEvent(port, VIRTIO_CONSOLE_CONSOLE_PORT, 1);
  133. this.SendName(port, "virtio-" + port);
  134. this.SendEvent(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
  135. break;
  136. case VIRTIO_CONSOLE_PORT_OPEN:
  137. this.Ack(queue_id, bufchain);
  138. if (port == 0) {
  139. this.SendWindowSize(port);
  140. }
  141. break;
  142. default:
  143. dbg_assert(false," VirtioConsole received unknown event: " + event[1]);
  144. return;
  145. }
  146. }
  147. },
  148. ],
  149. },
  150. isr_status:
  151. {
  152. initial_port: 0xB700,
  153. },
  154. device_specific:
  155. {
  156. initial_port: 0xB600,
  157. struct:
  158. [
  159. {
  160. bytes: 2,
  161. name: "cols",
  162. read: () => this.cols,
  163. write: data => { /* read only */ },
  164. },
  165. {
  166. bytes: 2,
  167. name: "rows",
  168. read: () => this.rows,
  169. write: data => { /* read only */ },
  170. },
  171. {
  172. bytes: 4,
  173. name: "max_nr_ports",
  174. read: () => this.ports,
  175. write: data => { /* read only */ },
  176. },
  177. {
  178. bytes: 4,
  179. name: "emerg_wr",
  180. read: () => 0,
  181. write: data => {
  182. dbg_assert(false, "Emergency write!");
  183. },
  184. },
  185. ]
  186. },
  187. });
  188. for (let port = 0; port < this.ports; ++port) {
  189. let queue_id = port == 0 ? 0 : port * 2 + 2;
  190. this.bus.register("virtio-console" + port + "-input-bytes", function(data) {
  191. let queue = this.virtio.queues[queue_id];
  192. if (queue.has_request()) {
  193. const bufchain = queue.pop_request();
  194. this.Send(queue_id, bufchain, new Uint8Array(data));
  195. } else {
  196. //TODO: Buffer
  197. }
  198. }, this);
  199. this.bus.register("virtio-console" + port + "-resize", function(size) {
  200. this.cols = size[0];
  201. this.rows = size[1];
  202. if (this.virtio.queues[2].is_configured() && this.virtio.queues[2].has_request()) {
  203. this.SendWindowSize(port);
  204. }
  205. }, this);
  206. }
  207. }
  208. VirtioConsole.prototype.SendWindowSize = function(port)
  209. {
  210. const bufchain = this.virtio.queues[2].pop_request();
  211. let buf = new Uint8Array(12);
  212. marshall.Marshall(["w", "h", "h", "h", "h"], [port, VIRTIO_CONSOLE_RESIZE, 0, this.rows, this.cols], buf, 0);
  213. this.Send(2, bufchain, buf);
  214. };
  215. VirtioConsole.prototype.SendName = function(port, name)
  216. {
  217. const bufchain = this.virtio.queues[2].pop_request();
  218. let namex = new TextEncoder().encode(name);
  219. let buf = new Uint8Array(8 + namex.length + 1);
  220. marshall.Marshall(["w", "h", "h"], [port, VIRTIO_CONSOLE_PORT_NAME, 1], buf, 0);
  221. for ( let i = 0; i < namex.length; ++i ) {
  222. buf[i+8] = namex[i];
  223. }
  224. buf[8 + namex.length] = 0;
  225. this.Send(2, bufchain, buf);
  226. };
  227. VirtioConsole.prototype.get_state = function()
  228. {
  229. let state = [];
  230. state[0] = this.virtio;
  231. state[1] = this.rows;
  232. state[2] = this.cols;
  233. state[3] = this.ports;
  234. return state;
  235. };
  236. VirtioConsole.prototype.set_state = function(state)
  237. {
  238. this.virtio.set_state(state[0]);
  239. this.rows = state[1];
  240. this.cols = state[2];
  241. this.ports = state[3];
  242. };
  243. VirtioConsole.prototype.Reset = function() {
  244. };
  245. VirtioConsole.prototype.SendEvent = function(port, event, value)
  246. {
  247. let queue = this.virtio.queues[2];
  248. const bufchain = queue.pop_request();
  249. let buf = new Uint8Array(8);
  250. marshall.Marshall(["w","h","h"], [port, event, value], buf, 0);
  251. this.Send(2, bufchain, buf);
  252. };
  253. VirtioConsole.prototype.Send = function (queue_id, bufchain, blob)
  254. {
  255. bufchain.set_next_blob(blob);
  256. this.virtio.queues[queue_id].push_reply(bufchain);
  257. this.virtio.queues[queue_id].flush_replies();
  258. };
  259. VirtioConsole.prototype.Ack = function (queue_id, bufchain)
  260. {
  261. bufchain.set_next_blob(new Uint8Array(0));
  262. this.virtio.queues[queue_id].push_reply(bufchain);
  263. this.virtio.queues[queue_id].flush_replies();
  264. };