|
| 1 | +// Copyright 2016-present runtime.js project authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +'use strict'; |
| 16 | +const VirtioDevice = require('./device'); |
| 17 | +const runtime = require('../../core'); |
| 18 | +const { Uint64LE } = require('int64-buffer'); |
| 19 | + |
| 20 | +const VIRTIO_BLK_T_IN = 0; |
| 21 | +const VIRTIO_BLK_T_OUT = 1; |
| 22 | + |
| 23 | +function initializeBlockDevice(pciDevice) { |
| 24 | + const ioSpace = pciDevice.getBAR(0).resource; |
| 25 | + const irq = pciDevice.getIRQ(); |
| 26 | + |
| 27 | + const features = { |
| 28 | + VIRTIO_BLK_F_SIZE_MAX: 1, |
| 29 | + VIRTIO_BLK_F_SEG_MAX: 2, |
| 30 | + VIRTIO_BLK_F_GEOMETRY: 4, |
| 31 | + VIRTIO_BLK_F_RO: 5, |
| 32 | + VIRTIO_BLK_F_BLK_SIZE: 6, |
| 33 | + VIRTIO_BLK_F_FLUSH: 9, |
| 34 | + VIRTIO_BLK_F_TOPOLOGY: 10, |
| 35 | + VIRTIO_BLK_F_CONFIG_WCE: 11, |
| 36 | + }; |
| 37 | + |
| 38 | + const dev = new VirtioDevice('blk', ioSpace); |
| 39 | + dev.setDriverAck(); |
| 40 | + |
| 41 | + const driverFeatures = { |
| 42 | + // VIRTIO_BLK_F_SIZE_MAX: true, |
| 43 | + VIRTIO_BLK_F_SEG_MAX: true, |
| 44 | + VIRTIO_BLK_F_GEOMETRY: true, |
| 45 | + VIRTIO_BLK_F_BLK_SIZE: true, |
| 46 | + VIRTIO_BLK_F_TOPOLOGY: true, |
| 47 | + }; |
| 48 | + |
| 49 | + const deviceFeatures = dev.readDeviceFeatures(features); |
| 50 | + debug(JSON.stringify(deviceFeatures)); |
| 51 | + |
| 52 | + if (!dev.writeGuestFeatures(features, driverFeatures, deviceFeatures)) { |
| 53 | + debug('[virtio] blk driver is unable to start'); |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + const QUEUE_ID_REQ = 0; |
| 58 | + |
| 59 | + const reqQueue = dev.queueSetup(QUEUE_ID_REQ); |
| 60 | + const promiseQueue = []; |
| 61 | + |
| 62 | + const sectorSize = 512; |
| 63 | + const sectorCount = dev.blkReadSectorCount(); |
| 64 | + const totalSectorCount = dev.blkReadTotalSectorCount(); |
| 65 | + |
| 66 | + function buildHeader(type, sector) { |
| 67 | + const u8 = new Uint8Array(16); |
| 68 | + const view = new DataView(u8.buffer); |
| 69 | + view.setUint32(0, type, true); |
| 70 | + view.setUint32(4, 0, true); // priority: low |
| 71 | + u8.set((new Uint64LE(sector)).toArray(), 8); |
| 72 | + return u8; |
| 73 | + } |
| 74 | + |
| 75 | + const diskDriver = new runtime.block.BlockDeviceInterface('virtio', { |
| 76 | + read(sector, data) { |
| 77 | + return new Promise((resolve, reject) => { |
| 78 | + if (sector > totalSectorCount) { |
| 79 | + reject(new RangeError(`sector ${sector} out of bounds (max ${totalSectorCount}, non-inclusive)`)); |
| 80 | + return; |
| 81 | + } |
| 82 | + const status = new Uint8Array(1); |
| 83 | + promiseQueue.push([resolve, reject, VIRTIO_BLK_T_IN, data, status]); |
| 84 | + reqQueue.placeBuffers([buildHeader(VIRTIO_BLK_T_IN, sector), data, status], false, [false, true, true]); |
| 85 | + |
| 86 | + if (reqQueue.isNotificationNeeded()) { |
| 87 | + dev.queueNotify(QUEUE_ID_REQ); |
| 88 | + } |
| 89 | + }); |
| 90 | + }, |
| 91 | + write(sector, data) { |
| 92 | + return new Promise((resolve, reject) => { |
| 93 | + const status = new Uint8Array(1); |
| 94 | + promiseQueue.push([resolve, reject, VIRTIO_BLK_T_OUT, data, status]); |
| 95 | + reqQueue.placeBuffers([buildHeader(VIRTIO_BLK_T_OUT, sector), data, status], false, [false, false, true]); |
| 96 | + |
| 97 | + if (reqQueue.isNotificationNeeded()) { |
| 98 | + dev.queueNotify(QUEUE_ID_REQ); |
| 99 | + } |
| 100 | + }); |
| 101 | + }, |
| 102 | + formatInfo: { |
| 103 | + sectorSize, |
| 104 | + sectorCount, |
| 105 | + totalSectorCount, |
| 106 | + }, |
| 107 | + isOnline() { |
| 108 | + return true; // TODO: actually check if the disk is online or not |
| 109 | + }, |
| 110 | + }); |
| 111 | + |
| 112 | + runtime.block.registerDevice(diskDriver); |
| 113 | + |
| 114 | + function recvBuffer() { |
| 115 | + if (promiseQueue.length === 0) { |
| 116 | + return; |
| 117 | + } |
| 118 | + const [resolve, reject, type, data, status] = promiseQueue.shift(); |
| 119 | + setImmediate(() => { |
| 120 | + if (status[0] !== 0) return reject(new Error('IO error')); |
| 121 | + if (type === VIRTIO_BLK_T_IN) { |
| 122 | + resolve(data); |
| 123 | + } else { |
| 124 | + resolve(); |
| 125 | + } |
| 126 | + }); |
| 127 | + } |
| 128 | + |
| 129 | + irq.on(() => { |
| 130 | + if (!dev.hasPendingIRQ()) { |
| 131 | + return; |
| 132 | + } |
| 133 | + reqQueue.fetchBuffers(recvBuffer); |
| 134 | + }); |
| 135 | + |
| 136 | + dev.setDriverReady(); |
| 137 | +} |
| 138 | + |
| 139 | +module.exports = initializeBlockDevice; |
0 commit comments