Annotation of nono/vm/virtio_def.cpp, revision 1.1.1.3

1.1       root        1: //
                      2: // nono
                      3: // Copyright (C) 2024 nono project
                      4: // Licensed under nono-license.txt
                      5: //
                      6: 
                      7: //
                      8: // VirtIO 定義
                      9: //
                     10: 
                     11: #include "virtio_def.h"
                     12: #include "mystring.h"
                     13: 
                     14: /*static*/ std::string
                     15: VirtIO::DeviceIDStr(uint32 id)
                     16: {
                     17:        static const char * const idnames[] = {
                     18:                "invalid",
1.1.1.2   root       19:                "Network Device",       // 1
                     20:                "Block Device",         // 2
                     21:                "Console Device",       // 3
                     22:                "Entropy Device",       // 4
1.1.1.3 ! root       23:                "Balloon Device",       // 5
        !            24:                "6",                            // 6
        !            25:                "7",                            // 7
        !            26:                "SCSI Device",          // 8
1.1       root       27:        };
                     28:        assert(id < countof(idnames));
                     29:        return idnames[id];
                     30: }
                     31: 
                     32: /*static*/ std::string
                     33: VirtIO::StatusBits(uint32 status)
                     34: {
                     35:        static struct {
                     36:                uint32 bit;
                     37:                const char *name;
                     38:        } table[] = {
                     39:                { STATUS_FAILED,                "FAILED" },
                     40:                { STATUS_NEEDS_RESET,   "NEEDS_RESET" },
                     41:                { STATUS_FEATURES_OK,   "F_OK" },
                     42:                { STATUS_OK,                    "OK" },
                     43:                { STATUS_DRIVER,                "DRIVER" },
                     44:                { STATUS_ACKNOWLEDGE,   "ACK" },
                     45:        };
                     46: 
                     47:        if (status == STATUS_RESET) {
                     48:                return "(RESET)";
                     49:        }
                     50: 
                     51:        std::string str;
1.1.1.2   root       52:        for (; status != 0; ) {
                     53:                // lowest は立ってる一番下のビットだけ取り出したもの
                     54:                decltype(status) lowest = status & (-status);
                     55: 
                     56:                str += ',';
                     57:                bool found = false;
                     58:                for (int i = 0; i < countof(table); i++) {
                     59:                        if (table[i].bit == lowest) {
                     60:                                str += table[i].name;
                     61:                                found = true;
                     62:                                break;
                     63:                        }
1.1       root       64:                }
1.1.1.2   root       65:                if (found == false) {
                     66:                        str += string_format("b%u", __builtin_ctz(lowest));
                     67:                }
                     68: 
                     69:                status &= ~lowest;
1.1       root       70:        }
                     71: 
1.1.1.2   root       72:        if (str.empty()) {
                     73:                return "<>";
                     74:        }
1.1       root       75:        str[0] = '<';
                     76:        str += '>';
                     77:        return str;
                     78: }
                     79: 
                     80: std::string
                     81: VirtQDesc::ToStr() const
                     82: {
                     83:        std::string str;
                     84: 
                     85:        str = string_format("addr=$%08x len=$%08x flags=$%04x(%c%c%c) next=$%04x",
                     86:                addr, len, flag,
                     87:                ((flag & VIRTQ_DESC_F_INDIRECT) ? 'I' : '-'),
                     88:                ((flag & VIRTQ_DESC_F_WRITE) ? 'W' : 'R'),
                     89:                ((flag & VIRTQ_DESC_F_NEXT) ? 'N' : '-'),
                     90:                next);
                     91:        return str;
                     92: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.