Skip to content
This repository was archived by the owner on Jul 3, 2020. It is now read-only.

Commit 606ffcf

Browse files
committed
runtime.disk -> runtime.block, improved device access structure, minor fixes
1 parent 4cc7b1f commit 606ffcf

File tree

7 files changed

+53
-43
lines changed

7 files changed

+53
-43
lines changed

js/core/disk/disk-interface.js js/core/block/block-device-interface.js

+12-16
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,33 @@
1414

1515
'use strict';
1616

17-
const privates = new WeakMap();
17+
const busHandle = Symbol('bus');
18+
const formatInfoHandle = Symbol('formatInfo');
1819

19-
class DiskDriver {
20-
constructor(name = '', init = {}) {
21-
privates.set(this, {
22-
name,
23-
});
20+
class BlockDeviceInterface {
21+
constructor(bus = '', init = {}) {
22+
this[busHandle] = bus;
23+
this[formatInfoHandle] = {};
2424
this.onread = null;
2525
this.onwrite = null;
26-
this.ongetformatinfo = null;
2726
this.ongetonline = null;
28-
if (typeof init === 'object') {
27+
if (typeof init === 'object' && init !== null) {
2928
if (typeof init.read === 'function') {
3029
this.onread = init.read;
3130
}
3231
if (typeof init.write === 'function') {
3332
this.write = init.write;
3433
}
3534
if (typeof init.formatInfo === 'object') {
36-
this.ongetformatinfo = () => init.formatInfo;
35+
this[formatInfoHandle] = init.formatInfo;
3736
}
3837
if (typeof init.isOnline === 'function') {
3938
this.ongetonline = init.isOnline;
4039
}
4140
}
4241
}
43-
get name() {
44-
return privates.get(this).name;
42+
get bus() {
43+
return this[busHandle];
4544
}
4645
read(sector, u8) {
4746
if (!this.onread) {
@@ -56,10 +55,7 @@ class DiskDriver {
5655
return this.onwrite(sector, u8);
5756
}
5857
get formatInfo() {
59-
if (!this.ongetformatinfo) {
60-
throw new Error('driver was not initialized');
61-
}
62-
return this.ongetformatinfo();
58+
return this[formatInfoHandle];
6359
}
6460
get isOnline() {
6561
if (!this.ongetonline) {
@@ -69,4 +65,4 @@ class DiskDriver {
6965
}
7066
}
7167

72-
module.exports = DiskDriver;
68+
module.exports = BlockDeviceInterface;

js/core/disk/disks.js js/core/block/devices.js

+17-6
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,26 @@
1313
// limitations under the License.
1414

1515
'use strict';
16-
const availableDisks = Object.create(null);
16+
const availableBuses = Object.create(null); // not using a Map because the buses should be easy to access directly
17+
const availableDevices = [];
1718

1819
module.exports = {
19-
registerDisk(disk) {
20-
availableDisks[disk.name] = disk;
20+
registerDevice(device) {
21+
if (!availableBuses[device.bus]) availableBuses[device.bus] = [];
22+
const i = availableDevices.push(device) - 1;
23+
Object.assign(device, {
24+
get name() {
25+
return `${device.bus}${i}`;
26+
},
27+
});
28+
availableBuses[device.bus].push(device);
2129

22-
console.log(`[disk] registered disk ${disk.name}`);
30+
console.log(`[block] registered block device ${device.name}`);
2331
},
24-
getDisks() {
25-
return availableDisks;
32+
getDevices() {
33+
return availableDevices;
34+
},
35+
getBuses() {
36+
return availableBuses;
2637
},
2738
};

js/core/disk/index.js js/core/block/index.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@
1313
// limitations under the License.
1414

1515
'use strict';
16-
const DiskInterface = require('./disk-interface');
17-
const { registerDisk, getDisks } = require('./disks');
16+
const BlockDeviceInterface = require('./block-device-interface');
17+
const { registerDevice, getDevices, getBuses } = require('./devices');
1818

1919
module.exports = {
20-
DiskInterface,
21-
registerDisk,
22-
get disks() {
23-
return getDisks();
20+
BlockDeviceInterface,
21+
registerDevice,
22+
get devices() {
23+
return getDevices();
2424
},
25+
get buses() {
26+
return getBuses();
27+
}
2528
};

js/core/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const { allocator } = require('./resources');
1818
require('./polyfill');
1919

2020
const random = require('./random');
21-
const disk = require('./disk');
21+
const block = require('./block');
2222
const keyboard = require('./keyboard');
2323
const ps2 = require('./ps2');
2424
const pci = require('./pci');
@@ -29,7 +29,7 @@ class Runtime {
2929
constructor() {
3030
Object.assign(this, {
3131
random,
32-
disk,
32+
block,
3333
keyboard,
3434
pci,
3535
ps2,

js/driver/virtio/blk.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,16 @@ function initializeBlockDevice(pciDevice) {
7272
return u8;
7373
}
7474

75-
const diskDriver = new runtime.disk.DiskInterface('hda', {
75+
const diskDriver = new runtime.block.BlockDeviceInterface('virtio', {
7676
read(sector, data) {
7777
return new Promise((resolve, reject) => {
7878
if (sector > totalSectorCount) {
79-
setImmediate(() => {
80-
reject(new RangeError(`sector ${sector} out of bounds (max ${totalSectorCount}, non-inclusive)`));
81-
});
79+
reject(new RangeError(`sector ${sector} out of bounds (max ${totalSectorCount}, non-inclusive)`));
8280
return;
8381
}
8482
const status = new Uint8Array(1);
8583
promiseQueue.push([resolve, reject, VIRTIO_BLK_T_IN, data, status]);
86-
reqQueue.placeBuffers([buildHeader(VIRTIO_BLK_T_IN, sector), data, status], [false, true, true]);
84+
reqQueue.placeBuffers([buildHeader(VIRTIO_BLK_T_IN, sector), data, status], null, [false, true, true]);
8785

8886
if (reqQueue.isNotificationNeeded()) {
8987
dev.queueNotify(QUEUE_ID_REQ);
@@ -94,7 +92,7 @@ function initializeBlockDevice(pciDevice) {
9492
return new Promise((resolve, reject) => {
9593
const status = new Uint8Array(1);
9694
promiseQueue.push([resolve, reject, VIRTIO_BLK_T_OUT, data, status]);
97-
reqQueue.placeBuffers([buildHeader(VIRTIO_BLK_T_OUT, sector), data, status], [false, false, true]);
95+
reqQueue.placeBuffers([buildHeader(VIRTIO_BLK_T_OUT, sector), data, status], null, [false, false, true]);
9896

9997
if (reqQueue.isNotificationNeeded()) {
10098
dev.queueNotify(QUEUE_ID_REQ);
@@ -111,7 +109,7 @@ function initializeBlockDevice(pciDevice) {
111109
},
112110
});
113111

114-
runtime.disk.registerDisk(diskDriver);
112+
runtime.block.registerDevice(diskDriver);
115113

116114
function recvBuffer() {
117115
if (promiseQueue.length === 0) {

js/driver/virtio/vring/descriptor-table.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@ class DescriptorTable {
8484
* @param buffers {array} array of Uint8Array buffers
8585
* @param lengths {array} array of corresponding buffer lengths (same size as buffers)
8686
* @param isWriteOnly {bool} set writeOnly flag for each buffer
87+
* @param isWriteOnlyArray {array} optional array of writeOnly flags, one value for each buffer
8788
*/
88-
placeBuffers(buffers, lengths, isWriteOnly) {
89+
placeBuffers(buffers, lengths, isWriteOnly, isWriteOnlyArray) {
8990
const count = buffers.length;
9091
if (this.descriptorsAvailable < count) {
9192
return -1;
@@ -96,10 +97,10 @@ class DescriptorTable {
9697
for (let i = 0; i < count; ++i) {
9798
const d = buffers[i];
9899
let bufWriteOnly = false;
99-
if (typeof isWriteOnly === 'boolean') {
100-
bufWriteOnly = isWriteOnly;
100+
if (isWriteOnlyArray) {
101+
bufWriteOnly = isWriteOnlyArray[i];
101102
} else {
102-
bufWriteOnly = isWriteOnly[i];
103+
bufWriteOnly = isWriteOnly;
103104
}
104105
let flags = 0;
105106
if (count !== i + 1) {

js/driver/virtio/vring/index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@ class VRing {
8181
*
8282
* @param buffers {array} Array of buffers
8383
* @param isWriteOnly {bool} R/W buffers flag
84+
* @param isWriteOnlyArray {array} optional array of 'isWriteOnly's, one for each buffer
8485
*/
85-
placeBuffers(buffers, isWriteOnly) {
86+
placeBuffers(buffers, isWriteOnly, isWriteOnlyArray) {
8687
if (this.suppressInterrupts) {
8788
this.fetchBuffers(null);
8889
}
@@ -107,7 +108,7 @@ class VRing {
107108
}
108109
}
109110

110-
const first = this.descriptorTable.placeBuffers(pageSplitBuffers, lengths, isWriteOnly);
111+
const first = this.descriptorTable.placeBuffers(pageSplitBuffers, lengths, isWriteOnly, isWriteOnlyArray);
111112
if (first < 0) {
112113
debug('virtio: no descriptors\n');
113114
return false;

0 commit comments

Comments
 (0)