--- nono/lib/mainapp.cpp 2026/04/29 17:04:54 1.1.1.8 +++ nono/lib/mainapp.cpp 2026/04/29 17:05:47 1.1.1.22 @@ -4,16 +4,78 @@ // Licensed under nono-license.txt // +// +// CLI/GUI 共通のメイン部分 +// + #include "mainapp.h" +#include "accel_avx2.h" +#include "config.h" +#include "hostnet.h" +#include "logger.h" +#include "monitor.h" #include "mystring.h" -#include "netdriver_selector.h" +#include "mythread.h" +#include "uimessage.h" +#include "vm_luna.h" +#include "vm_news.h" +#include "vm_virt68k.h" +#include "vm_x68k.h" #include +#include +#include #include #include +#include +#if defined(__linux__) +#include +#endif // グローバルインスタンス MainApp gMainApp; +// コンストラクタ +MainApp::MainApp() +{ +} + +// デストラクタ +MainApp::~MainApp() +{ + // 逆順に解放 + + pVM.reset(); + + pConfig.reset(); + gConfig = NULL; + + pMonitorManager.reset(); + gMonitorManager = NULL; + + hostcpu.reset(); + +#if 0 // 確認用 + if (objects.empty() == false) { + printf("~MainApp: undead objects are:"); + for (const auto *obj : objects) { + printf(" %s", obj->GetIdStr()); + } + printf("\n"); + } +#endif + + logger.reset(); +} + +// VM を解放。 +void +MainApp::Dispose() +{ + if ((bool)pVM) { + pVM->Dispose(); + } +} + // ヘルプメッセージ void MainApp::ShowHelp(bool all) const @@ -23,30 +85,42 @@ MainApp::ShowHelp(bool all) const #define p(msg) printf(" " msg "\n") printf("usage: %s []\n", getprogname()); - p("-A load and execute host binary (a.out or ELF)"); p("-c vm directory or configuration file"); - p("-f fast mode"); + p("-f fast mode (same as '-V fast-mode=1')"); + p("--fd[01] specify floppy image (same as '-V fdN-image=' except"); + p(" base directory)"); if (IsGUI()) - p("--fontsize fontsize in monitors {12,16} (default:12)"); + p("--fontsize fontsize (same as '-V monitor-fontsize=')"); p("-h show brief help message"); p("--help show all help message including for developers"); if (IsGUI()) - p("-s,--scale screen scale, default 1.0"); + p("-s mainview scale (same as '-V mainview-scale=')"); p("--show-config show configuration variables"); p("--show-hostnet show list of hostnet drivers"); p("-v show version"); p("-V = overwrite config option"); + p("-X load and execute host binary (a.out or ELF)"); + p("--initrd initial ramdisk (only for virt68k)"); if (all) { printf("\n(options for developers)\n"); - p("-b [,] set breakpoint"); - p("-B benchmark mode"); + p("--b [,][,] (or -b)"); + p(" set breakpoint. is either 'main'(default) or 'xp'"); + p("--bi [,][:][,]"); + p(" set instruction breakpoint."); + p("--bi-exg set instruction breakpoint on 'exg sp,sp'"); + p("--bv [,][-][,]"); + p(" set vector breakpoint."); p("-C output log to console"); + p("--console-log specify the console output log"); p("-d debugger prompt on startup"); - p("-D debugger on console"); + p("-D same as '-V debugger-driver=stdio'"); + p("-H human68k console emulation"); p("-L =[,..] set loglevel (-Lhelp displays names)"); p("-M [,..] monitors to display at startup (-Mhelp displays names)"); - p("-X human68k console emulation"); + p(" memdump[=[.]] fmt := B/W/L/M(MMU)/I(Disasm)/Z(XPDisasm)"); + p("-S MSX-DOS console emulation"); + p("--perf performance measure mode"); } } @@ -54,15 +128,33 @@ MainApp::ShowHelp(bool all) const // enum は getopt() の1文字のオプションと衝突しなければいいので適当に // 0x80 から始めておく。 enum { - OPT_fontsize = 0x80, + OPTstart = 0x80 - 1, + OPT_bi, + OPT_bi_exg, + OPT_bv, + OPT_console_log, + OPT_fd0, + OPT_fd1, + OPT_fontsize, OPT_help, + OPT_initrd, + OPT_perf, OPT_show_config, OPT_show_config_all, OPT_show_hostnet, }; static struct option longopts[] = { + { "b", required_argument, NULL, 'b' }, + { "bi", required_argument, NULL, OPT_bi }, + { "bi-exg", no_argument, NULL, OPT_bi_exg }, + { "bv", required_argument, NULL, OPT_bv }, + { "console-log", required_argument, NULL, OPT_console_log }, + { "fd0", required_argument, NULL, OPT_fd0 }, + { "fd1", required_argument, NULL, OPT_fd1 }, { "fontsize", required_argument, NULL, OPT_fontsize }, { "help", no_argument, NULL, OPT_help }, + { "initrd", required_argument, NULL, OPT_initrd }, + { "perf", no_argument, NULL, OPT_perf }, { "show-config", no_argument, NULL, OPT_show_config }, // --show-config-all は開発用なのでヘルプには載せない { "show-config-all",no_argument, NULL, OPT_show_config_all }, @@ -77,128 +169,262 @@ static struct option longopts[] = { // MainApp::PASS .. 実行を継続(通常パス) // EXIT_SUCCESS / EXIT_FAILURE .. この終了コードでアプリケーションを終了 int -MainApp::Init1(bool is_cli_, int ac, char *av[]) +MainApp::Init1(int ac, char *av[], std::string (*conv)(const std::string&)) { - is_cli = is_cli_; + int rv; - if (!ParseOpt(ac, av)) { - return EXIT_FAILURE; + is_cli = (conv == NULL); + + rv = Init1a(ac, av, conv); + if (rv != MainApp::PASS) { + return rv; } - // ログ機構は引数処理後なるはや + rv = Init1b(); + return rv; +} + +// VM の初期化、ステージ1 の前半。 +// Config.Fix まで。 +int +MainApp::Init1a(int ac, char *av[], std::string (*conv)(const std::string&)) +{ + int rv; + +#if defined(__linux__) + // Linux ではケーパビリティが設定されていると coredump しないので + // 明示的に許可する必要がある + prctl(PR_SET_DUMPABLE, 1, 0, 0, 0); +#endif + + rv = ParseOpt(ac, av); + if (rv != MainApp::PASS) { + return rv; + } + + // モニタマネージャ (VM 作成より前、もしかしたら Config より前?) + pMonitorManager.reset(new MonitorManager()); + gMonitorManager = pMonitorManager.get(); + + // ログ機構は引数処理後なるはや…にしたいがモニタマネージャは必要。 // CLI ならログは常に標準出力へ(も)出力。 // GUI なら -C で指定。 - gLogger.UseStdout(IsCLI() ? true : log_to_console); + logger.reset(new Logger()); + if (IsCLI()) { + logger->UseStdout(true); + } else { + logger->UseStdout(log_to_console); + logger->SetConverter(conv); + } + logger->StartThread(); - // 設定を作成。コンストラクタで初期値を用意 - gConfig.reset(new Config()); + // UIMessage は VM 起動前ならどこでもいいはず。 + uimessage.reset(new UIMessage()); - // 引数で指定されたディレクトリの設定ファイルがあれば読み込んで設定を更新 - ConfigFile file(vmfile); - if (file.Load() == false) { + // 設定を作成。 + // c0. コンストラクタで初期値を用意。 + pConfig.reset(new Config()); + gConfig = pConfig.get(); + + // c1. ホームディレクトリに設定ファイルがあれば読み込んで設定を更新。 + struct passwd *passwd = getpwuid(getuid()); + // 見付からないことはないはずだが、一応。 + if (passwd != NULL) { + std::string filename = std::string(passwd->pw_dir) + "/.nono.cfg"; + ConfigFile dotfile(filename, ConfigItem::FromHome); + if (dotfile.Load()) { + if (gConfig->Update(dotfile) == false) { + return EXIT_FAILURE; + } + } + } + + // c2. VM ディレクトリに設定ファイルがあれば読み込んで設定を更新。 + // vmfile が存在するかどうかはここで調べるまで分からない。 + ConfigFile cfgfile(vmfile, ConfigItem::FromConfig); + if (cfgfile.Load() == false) { return EXIT_FAILURE; } - if (gConfig->Update(file) == false) { + if (gConfig->Update(cfgfile) == false) { return EXIT_FAILURE; } - // 最後にコマンドライン引数で更新 - if (gConfig->Update(config_options) == false) { + + // c3. 最後にコマンドライン引数で更新 + for (const auto& pair : config_options) { + if (gConfig->Update(pair.first, pair.second) == false) { + return EXIT_FAILURE; + } + } + + // c4. exec-file, exec-arg をここでメンバ変数 exec_file, exec_arg に代入。 + // (c3 までに確定していて、1. VM コンストラクト時には必要) + const ConfigItem& itemexec = gConfig->Find("exec-file"); + const std::string& execstr = itemexec.AsString(); + if (execstr.empty() == false) { + // 歴史的経緯で exec_file は const char *。 + exec_file = strdup(NormalizePath(execstr).c_str()); + } + const ConfigItem& itemarg = gConfig->Find("exec-arg"); + exec_arg = itemarg.AsString(); + + // Human68k/MSX-DOS モードならここでいくつかパラメータを強制的に変更。 + if (human_mode) { + // X68030 最小構成でいい。 + gConfig->Update("vmtype=x68030", "-H"); + } + if (msxdos_mode) { + // LUNA-I 最小構成でいい。 + gConfig->Update("vmtype=luna", "-S"); + gConfig->Update("ram-size=16", "-S"); + gConfig->Update("luna-video-plane=1", "-S"); + // ホスト側には干渉しない。 + gConfig->Update("hostcom0-driver=none", "-S"); + gConfig->Update("hostcom1-driver=none", "-S"); + gConfig->Update("hostcom2-driver=none", "-S"); + gConfig->Update("hostnet-driver=none", "-S"); + } + + // ログ表示のためだけのオブジェクト + hostcpu.reset(new Object(OBJ_HOSTCPU)); + + // ホスト CPU の機能チェック。 + if (CheckCPU() == false) { return EXIT_FAILURE; } - // 1. VM 種別を決定 - if (IsCLI() && human68k_file) { - vmtype = VMTYPE_RXZ; + // 0. VM 種別を決定 + // VM 種別文字列から vmtype を決定 + const ConfigItem& item = gConfig->Find("vmtype"); + vmstr = string_tolower(item.AsString()); + if (vmstr == "x68030") { + vmtype = VMType::X68030; + } else if (vmstr == "luna") { + vmtype = VMType::LUNA1; + } else if (vmstr == "luna88k") { + vmtype = VMType::LUNA88K; + } else if (vmstr == "news") { + vmtype = VMType::NEWS; + } else if (vmstr == "virt-m68k" || vmstr == "virt68k") { + vmtype = VMType::VIRT68K; } else { - // VM 種別文字列から vmtype を決定 - const ConfigItem& item = gConfig->Find("vmtype"); - std::string vmstr = string_tolower(item.AsString()); - if (vmstr == "x68030") { - vmtype = VMTYPE_X68030; - } else if (vmstr == "luna") { - vmtype = VMTYPE_LUNA1; - } else if (vmstr == "luna88k") { - vmtype = VMTYPE_LUNA88K; + if (item.GetFrom() == ConfigItem::FromInitial) { + // 未指定の時 + warnx("vmtype must be specified"); } else { - if (item.GetFrom() == ConfigItem::FromInitial) { - // 未指定の時 - warnx("vmtype must be specified"); - } else { - // ユーザ由来の時 - item.Err("invalid vmtype"); - } - return EXIT_FAILURE; + // ユーザ由来の時 + item.Err("Invalid vmtype"); } + return EXIT_FAILURE; } - // 2. VM 作成 + // 1. VM 作成 switch (vmtype) { - case VMTYPE_X68030: - gVM.reset(new VM_X68030()); + case VMType::X68030: + pVM.reset(new VM_X68030()); + break; + + case VMType::LUNA1: + pVM.reset(new VM_LUNA1()); break; - case VMTYPE_LUNA1: - gVM.reset(new VM_LUNA1()); + case VMType::LUNA88K: + pVM.reset(new VM_LUNA88K()); break; - case VMTYPE_RXZ: - gVM.reset(new VM_RXZ()); + case VMType::NEWS: + pVM.reset(new VM_NEWS()); break; - case VMTYPE_LUNA88K: - gVM.reset(new VM_LUNA88K()); + case VMType::VIRT68K: + pVM.reset(new VM_Virt68k()); break; default: - __unreachable(); + PANIC("vmtype=%s not configured", vmstr.c_str()); } - // 3. 動的なコンストラクション - if (!gVM->Create()) { - gVM.reset(); + // 2. 動的なコンストラクションその1 + // 3. ログ出力用の EarlyInit + if (!pVM->Create(VM::CreationPhase::First)) { + pVM.reset(); return EXIT_FAILURE; } - // デバッガ (ここでもモニタを登録する) - debugger_create(); - - // 設定内容を表示して終了 + // 4. 設定を確定。 // (VM コンストラクタで変数の増減があるのでそれより後、 // Create() でも SCSI パラメータを減らすので、それより後) - gConfig->Fix(); + if (gConfig->Fix() == false) { + pVM.reset(); + return EXIT_SUCCESS; + } + + // Fix() は配列から削除を行うため、これより前に取得していた ConfigItem + // のポインタや参照は無効になっている可能性がある。Fix() をまたいで + // ポインタや参照を使ってしまわないようここで一旦フォーカスを抜ける。 + // なお Fix() 後は要素の削除は出来ないため、以降はこの問題は発生しない。 + + return PASS; +} + +// VM の初期化、ステージ1 の後半。 +// Config.Fix 以降。 +int +MainApp::Init1b() +{ + // 4.1 設定が確定したので --show-config なら設定内容を表示して終了。 if (show_config) { gConfig->Show(show_config - 1); - gVM.reset(); + pVM.reset(); return EXIT_SUCCESS; } - // 4. ログレベルを設定。コンストラクト後すぐに行う。 + // 4.2 ログレベルを設定。コンストラクト後すぐに行う。 // -L help もここで処理。 if (!ParseLogopt()) { - gVM.reset(); + pVM.reset(); return EXIT_FAILURE; } - // 5. ホストネットワークドライバの作成。 - // - // ホストネットワークドライバを作成する時にはログ表示が欲しいが、通常の - // フローではオブジェクトが出揃った後でログレベルを割り当てるので、 - // にわたま問題になってしまう。そこでこれだけ特別対応する。 - // 手順 3. のコンストラクションで EthernetDevice が NetDriverSelector を - // 作成し、手順 4. で通常通りにログレベルの割り当てを受ける。このログを - // 使ってここ(手順5)で実際の NetDriver を作成し、同時にログレベルも引き - // 継ぐことにする。 - if ((bool)gNetDriverSelector) { - // 実際の NetDriver を作成 - NetDriver *newdriver = gNetDriverSelector->InitDriver(); - if (newdriver == NULL) { - gVM.reset(); + // 4.3 -X のチェック。 + // -L help より後にしないとヘルプが表示できない。 + const ConfigItem& itemexec = gConfig->Find("exec-file"); + if (exec_file == NULL) { + // -X が必要なケースで指定されてなければエラー。 + + // 実行ファイル指定が必須の VM。 + if (vmtype == VMType::NEWS || vmtype == VMType::VIRT68K) { + if (itemexec.GetFrom() == ConfigItem::FromInitial) { + warnx("vmtype=%s requires -X option (or exec-file).", + vmstr.c_str()); + } else { + itemexec.Err("filename must be specified."); + } + return EXIT_FAILURE; + } + + // Human モード、MSX-DOS モードでも実行ファイル名が必要。 + if (human_mode) { + warnx("-H option needs -X"); return EXIT_FAILURE; } - // 成功すればセット - gNetDriver.reset(newdriver); + if (msxdos_mode) { + warnx("-S option needs -X"); + return EXIT_FAILURE; + } + } else { + // 実行ファイルのファイル名を間違えたくらいならここでエラーに出来る。 + int r = access(exec_file, R_OK); + if (r != 0) { + itemexec.Err("%s", strerror(errno)); + return EXIT_FAILURE; + } + } - // NetDriverSelector はこれ以降まったく使わないがとりあえず放置。 + // 5. 動的コンストラクションその2 + // ログ出力が出来る代わりにログクラスはもう増やせない。 + if (!pVM->Create(VM::CreationPhase::Second)) { + pVM.reset(); + return EXIT_FAILURE; } return PASS; @@ -211,19 +437,16 @@ MainApp::Init1(bool is_cli_, int ac, cha bool MainApp::Init2() { - // VM 初期化。 - // これ以降はスレッドを開始したかもしれないので false を返す際には - // VM をデストラクトすること。 - if (!gVM->Init()) { - gVM.reset(); + // 7. VM 初期化。 + if (!pVM->Init()) { return false; } - // デバッガは VM オブジェクトリストに入っていない - debugger_init(); - // 起動時設定の適用 - if (!gVM->Apply()) { - gVM.reset(); + // メインスレッド名を設定 + PTHREAD_SETNAME("Main"); + + // 8. スレッド開始 + if (!pVM->StartThread()) { return false; } @@ -232,43 +455,29 @@ MainApp::Init2() // コマンドライン引数を処理する。 // 知らない引数とかがあればこちらで usage を表示して false を返す。 -bool +// 戻り値は、MainApp::PASS なら実行を継続、 +// EXIT_SUCCESS/EXIT_FAILURE ならこのコードで終了。 +int MainApp::ParseOpt(int ac, char *av[]) { struct stat st; const char *cpath; - int b; int c; - fontsize = 12; - screen_scale = 1.0; - cpath = "."; + cpath = NULL; - while ((c = getopt_long(ac, av, "A:b:B:c:CdDfhL:M:s:vV:X:", + while ((c = getopt_long(ac, av, "b:c:CdDfhHL:M:s:SvV:X:", longopts, NULL)) != -1) { switch (c) { - case 'A': - host_file = optarg; - break; - case 'b': debug_breakaddr.push_back(optarg); break; - case 'B': - b = atoi(optarg); - if (b < 2 || b > 6) { - fprintf(stderr, "-B : 2..6\n"); - return false; - } - benchmark_mode = b; - break; - case 'c': cpath = optarg; // 空文字列なら再び初期値に if (cpath[0] == '\0') { - cpath = "."; + cpath = NULL; } break; @@ -281,18 +490,33 @@ MainApp::ParseOpt(int ac, char *av[]) break; case 'D': - debug_on_console = true; + // -D は -V debugger-driver=stdio と等価。 + config_options.emplace_back("debugger-driver=stdio", "-D"); break; case 'f': - fast_mode = true; + // -f は -V fast-mode=1 と等価。 + config_options.emplace_back("fast-mode=1", "-f"); + break; + + case OPT_fd0: + case OPT_fd1: + { + int fd = c - OPT_fd0; + // 相対パスはカレントディレクトリを起点とする。 + std::string line = string_format("fd%u-image=%s", fd, + AbsPath(optarg).c_str()); + std::string from = string_format("--fd%u", fd); + config_options.emplace_back(line, from); + break; + } + + case 'H': + human_mode = true; break; case 'L': - if (logopt[0] != '\0') { - strlcat(logopt, ",", sizeof(logopt)); - } - strlcat(logopt, optarg, sizeof(logopt)); + AddLogopt(optarg); break; case 'M': @@ -303,48 +527,89 @@ MainApp::ParseOpt(int ac, char *av[]) break; case 's': - if (IsGUI()) { - char *end; - errno = 0; - screen_scale = strtod(optarg, &end); - if (end == optarg || end[0] != '\0' || errno == ERANGE) { - warnx("-s: invalid argument"); - return false; - } - // 上限は適当 - if (screen_scale <= 0.0 || screen_scale >= 10.0) { - warnx("-s: invalid scale"); - return false; - } - } + { + auto line = string_format("mainview-scale=%s", optarg); + config_options.emplace_back(line, "-s"); + break; + } + + case 'S': + msxdos_mode = true; break; case 'X': - human68k_file = optarg; - for (int i = optind; i < ac; i++) { - if (i != optind) { - strlcat(human68k_arg, " ", sizeof(human68k_arg)); + { + // 相対パスはカレントディレクトリ起点とする。 + std::string line = "exec-file=" + AbsPath(optarg); + config_options.emplace_back(line, "-X"); + + // Human68k モードなら後続を引数にする。 + // UNIX モードでは親和性が悪いのでしない。 + if (human_mode) { + std::string arg; + arg = "exec-arg="; + for (int i = optind; i < ac; i++) { + if (i != optind) { + arg += " "; + } + arg += av[i]; } - strlcat(human68k_arg, av[i], sizeof(human68k_arg)); + optind = ac; + config_options.emplace_back(arg, "-X"); } - optind = ac; break; + } case 'v': ShowVersion(); exit(0); case 'V': - config_options.push_back(optarg); + config_options.emplace_back(optarg, "-V"); + break; + + case OPT_bi: + debug_breakinst.push_back(optarg); + break; + + case OPT_bi_exg: + debug_breakinst_exg = true; + break; + + case OPT_bv: + debug_breakvec.push_back(optarg); + break; + + case OPT_console_log: + // この時点ではコンソールを持つ VM かどうかが分からないので + // コンソールのない設定でもエラーを出さない(出せない)。 + console_logfile = optarg; break; case OPT_fontsize: - fontsize = atoi(optarg); + { + auto line = string_format("monitor-fontsize=%s", optarg); + config_options.emplace_back(line, "--fontsize"); break; + } case OPT_help: ShowHelp(true); - return false; + return EXIT_SUCCESS; + + case OPT_initrd: + { + // 相対パスはカレントディレクトリを起点とする。 + std::string line = "exec-initrd=" + AbsPath(optarg); + config_options.emplace_back(line, "--initrd"); + break; + } + + case OPT_perf: + // パフォーマンス確認用。(Config::Update() 側で展開する) + config_options.emplace_back("--perf", ""); + log_to_console = true; + break; case OPT_show_config: show_config = 1; @@ -356,42 +621,57 @@ MainApp::ParseOpt(int ac, char *av[]) case OPT_show_hostnet: ShowHostnet(); - return false; + return EXIT_SUCCESS; case 'h': + ShowHelp(false); + return EXIT_SUCCESS; + default: ShowHelp(false); - return false; + return EXIT_FAILURE; } } - // 引数がディレクトリなら、それを VM ディレクトリとし、その中の - // nono.cfg を設定ファイルとする。 - // 引数がファイルなら、それを設定ファイルとし、そのファイルがある - // ディレクトリを VM ディレクトリとする。 - // -c DIR => vmdir = DIR, vmfile = DIR/nono.cfg - // -c DIR/FILE => vmdir = DIR, vmfile = FILE - if (stat(cpath, &st) < 0) { - warn("stat %s", cpath); - return false; + if (human_mode) { + // Human モードで -c ありなら、VM ディレクトリ指定。 + // Human モードで -c 省略なら、VM ディレクトリなし (cpath==NULL)。 + } else { + // Human モードでなければ、-c 省略は -c . と同義。 + if (cpath == NULL) { + cpath = "."; + } } - if (S_ISDIR(st.st_mode)) { - vmdir = std::string(cpath); - if (vmdir.back() != '/') { - vmdir += '/'; - } - vmfile = vmdir + "nono.cfg"; - } else if (S_ISREG(st.st_mode)) { - vmfile = std::string(cpath); - auto pos = vmfile.rfind('/'); - if (pos != std::string::npos) { - vmdir = vmfile.substr(0, pos + 1); + + if (cpath != NULL) { + // 引数がディレクトリなら、それを VM ディレクトリとし、その中の + // nono.cfg を設定ファイルとする。 + // 引数がファイルなら、それを設定ファイルとし、そのファイルがある + // ディレクトリを VM ディレクトリとする。 + // -c DIR => vmdir = DIR, vmfile = DIR/nono.cfg + // -c DIR/FILE => vmdir = DIR, vmfile = FILE + if (stat(cpath, &st) < 0) { + warn("stat %s", cpath); + return EXIT_FAILURE; + } + if (S_ISDIR(st.st_mode)) { + vmdir = std::string(cpath); + if (vmdir.back() != '/') { + vmdir += '/'; + } + vmfile = vmdir + "nono.cfg"; + } else if (S_ISREG(st.st_mode)) { + vmfile = std::string(cpath); + auto pos = vmfile.rfind('/'); + if (pos != std::string::npos) { + vmdir = vmfile.substr(0, pos + 1); + } else { + vmdir = "./"; + } } else { - vmdir = "./"; + warnx("-c %s: path must be file or directory", cpath); + return EXIT_FAILURE; } - } else { - warnx("-c %s: path must be file or directory", cpath); - return false; } // CLI 版で -Mhelp つけても黙って起動するのはさすがにどうかと思う。 @@ -401,17 +681,199 @@ MainApp::ParseOpt(int ac, char *av[]) if (IsCLI()) { if (monitor_opt == "help") { warnx("-Mhelp is not available on CLI"); - return false; + return EXIT_FAILURE; } if (!monitor_opt.empty()) { warnx("-M option is ignored on CLI"); - return true; + return EXIT_FAILURE; } } + return MainApp::PASS; +} + +// path を絶対パスにして返す。 +// 相対パスならカレントディレクトリからのパスとする。 +/*static*/ std::string +MainApp::AbsPath(const char *path) +{ + char cwd[PATH_MAX]; + + getcwd(cwd, sizeof(cwd)); + return NormalizePath(optarg, std::string(cwd)); +} + +// CPU の機能とかをチェック。 +bool +MainApp::CheckCPU() +{ + const ConfigItem& item_aff = gConfig->Find(".host-cpu-affinity"); + std::string cfg_aff = item_aff.AsString(); + + if (cfg_aff.empty() || cfg_aff == "auto") { + // 指定なし + // (auto も今の所指定なしと同じ) + cpu_affinity = CPUAffinity::None; + } else if (cfg_aff.find(':') == std::string::npos) { + item_aff.Err(); + } else { + // 書式は ":"。 + // は "low", "high", "perf" のいずれか。 + // は "0,3-5,7" みたいな感じ。 + std::vector vals = string_split(cfg_aff, ':'); + std::string key = string_trim(vals[0]); + std::string csv = vals[1]; + if (key == "low") { + cpu_affinity = CPUAffinity::Low; + } else if (key == "high") { + cpu_affinity = CPUAffinity::High; + } else if (key == "perf") { + cpu_affinity = CPUAffinity::Perf; + } else { + item_aff.Err("Invalid affinity keyword"); + } + cpu_list.resize(std::thread::hardware_concurrency()); + auto errmsg = ParseCPUList(cpu_list, csv); + if (errmsg.empty() == false) { + item_aff.Err(errmsg); + return false; + } + + // 全部 true/false なら、これは意味がないのでクリアする。 + uint num = std::count(cpu_list.begin(), cpu_list.end(), true); + if (num == 0) { + item_aff.Err("No valid cores specified (Ignoring it)"); + cpu_affinity = CPUAffinity::None; + } else if (num == cpu_list.size()) { + item_aff.Err("All cores specified (Ignoring it)"); + cpu_affinity = CPUAffinity::None; + } + } + +#if defined(HAVE_AVX2) + // AVX2 + detect_avx2 = ::DetectAVX2(); + + const ConfigItem& item_avx2 = gConfig->Find("host-avx2"); + std::string cfg_avx2 = item_avx2.AsString(); + + if (cfg_avx2 == "auto") { + enable_avx2 = detect_avx2; + } else if (cfg_avx2 == "no") { + enable_avx2 = false; + } else { + item_avx2.Err("must be either \"auto\" or \"no\"."); + return false; + } +#endif + +#if defined(HAVE_NEON) + // NEON は aarch64 なら必ずある? + detect_neon = true; + + const ConfigItem& item_neon = gConfig->Find("host-neon"); + std::string cfg_neon = item_neon.AsString(); + + if (cfg_neon == "auto") { + enable_neon = detect_neon; + } else if (cfg_neon == "no") { + enable_neon = false; + } else { + item_neon.Err("must be either \"auto\" or \"no\"."); + return false; + } +#endif + + // 何もしない。設定のチェックもしないほうがいい。 return true; } +// CPU 番号リストの文字列からビット配列を作って返す。 +// cpulist は CPU コア数で初期化(resize)しておくこと。 +// 成功すれば string.empty を返す。 +// 失敗すればエラーメッセージを返す。 +std::string +MainApp::ParseCPUList(std::vector& cpulist, const std::string& input) +{ + if (input.empty()) { + return " must be specified"; + } + + // 設定は "0,3-5,7,10-" のような書式 + std::vector csv = string_split(input, ','); + for (auto& r : csv) { + const char *str; + char *endp; + uint start, last; + + if (r.empty()) { + // 空なら無視する? + continue; + } else if (r.find('-') == std::string::npos) { + // '-' がなければ単独 + str = &r[0]; + errno = 0; + start = strtoul(str, &endp, 10); + if (endp == str || *endp != '\0' || errno == ERANGE) { + return "syntax error"; + } + last = start; + } else if (r[0] == '-') { + // "-a" なら範囲 [0, a] + str = &r[1]; + errno = 0; + last = strtoul(str, &endp, 10); + if (endp == str || *endp != '\0' || errno == ERANGE) { + return "syntax error"; + } + start = 0; + } else if (r[r.size() - 1] == '-') { + // "a-" なら範囲 [a, MAX] + str = &r[0]; + errno = 0; + start = strtoul(str, &endp, 10); + if (endp == str || endp != &r[r.size() - 1] || errno == ERANGE) { + return "syntax error"; + } + last = cpulist.size(); + } else { + // "a-b" なら範囲 [a, b] + str = &r[0]; + errno = 0; + start = strtoul(str, &endp, 10); + if (endp == str || *endp != '-' || errno == ERANGE) { + return "syntax error"; + } + + str = endp + 1; + last = strtoul(str, &endp, 10); + if (endp == str || *endp != '\0' || errno == ERANGE) { + return "syntax error"; + } + + if (start > last) { + uint tmp = start; + start = last; + last = tmp; + } + } + + if (start >= cpulist.size()) { + return string_format("cpu number %u exceeds number of cores(%zu)", + start, cpulist.size()); + } + if (last >= cpulist.size()) { + return string_format("cpu number %u exceeds number of cores(%zu)", + start, cpulist.size()); + } + for (uint n = start; n <= last; n++) { + cpulist[n] = true; + } + } + + return ""; +} + // バージョンを表示 void MainApp::ShowVersion() const @@ -421,6 +883,16 @@ MainApp::ShowVersion() const NONO_MAJOR_VER, NONO_MINOR_VER, NONO_PATCH_VER, NONO_DATE); } +// ログレベル指定文字列を logopt に追加する。 +void +MainApp::AddLogopt(const char *opt) +{ + if (logopt.empty() == false) { + logopt += ','; + } + logopt += opt; +} + // ログレベル指定文字列を処理する。 // str はログレベル指定文字列を ',' で連結した "foo=1,bar=2" 形式の文字列で、 // これを分解してそれぞれ担当するオブジェクトのログレベルにセットする。 @@ -431,7 +903,7 @@ MainApp::ParseLogopt() std::vector items; // 分解して.. - items = string_split(logopt, ','); + items = string_split(logopt.c_str(), ','); // "help" があればヘルプを表示して終了 for (const auto& item : items) { @@ -470,7 +942,7 @@ MainApp::SetLogopt(const std::vectorGetName().c_str(), o->loglevel); } } @@ -508,37 +980,85 @@ MainApp::SetLogopt1(const std::string& i return false; } + // 今の所、値域は -1 〜 9 ということにしておく。 + if (level < -1) { + level = -1; + } else if (level > 9) { + level = 9; + } + // "all" なら全部にセット if (name == "all") { - for (auto obj : gObjects) { + for (auto obj : gMainApp.GetObjects()) { if (obj->GetName().empty() == false) { - obj->loglevel = level; + obj->SetLogLevel(level); } } return true; } - std::vector found; // 候補オブジェクト - std::vector candidates; // 候補名 - - // 前方一致する名前(とそのオブジェクト)を列挙する - for (auto *obj : gObjects) { + // "" なら "*" 全部に展開する。 +#define EXPAND(key) do { \ + if (name == key) { \ + for (auto obj : gMainApp.GetObjects()) { \ + if (strncasecmp(obj->GetName().c_str(), key, strlen(key)) == 0) \ + obj->SetLogLevel(level); \ + } \ + return true; \ + } \ +} while (0) + EXPAND("bankram"); + EXPAND("fdd"); + EXPAND("gfpic"); + EXPAND("hostcom"); + EXPAND("hostnet"); + + // エイリアスリストを作る + using aliaslist_t = std::vector>; + aliaslist_t alias_list; + for (const auto& obj : gMainApp.GetObjects()) { const std::vector& aliases = obj->GetAliases(); for (const auto& a : aliases) { - // 完全一致したら即採用 - // (前方一致しつつ完全一致したらこの時点で採用して打ち切るため) - if (strcasecmp(a.c_str(), name.c_str()) == 0) { - obj->loglevel = level; - return true; - } + alias_list.emplace_back(a, obj); + } + } - // 前方一致したら覚えておく - if (strncasecmp(a.c_str(), name.c_str(), name.size()) == 0) { - candidates.emplace_back(a); - found.push_back(obj); + // エイリアスを完全一致のみのものと部分一致も許容するものに分ける + aliaslist_t exact_alias; + aliaslist_t partial_alias; + for (const auto& a0 : alias_list) { + bool exact = false; + for (const auto& a1 : alias_list) { + if (a0.first == a1.first) { + continue; + } + if (starts_with_ignorecase(a0.first, a1.first)) { + exact = true; + break; } } + if (exact) { + exact_alias.push_back(a0); + } else { + partial_alias.push_back(a0); + } + } + + // 完全一致をまず調べる + for (const auto& a : exact_alias) { + if (strcasecmp(a.first.c_str(), name.c_str()) == 0) { + a.second->SetLogLevel(level); + return true; + } + } + + aliaslist_t found; + for (const auto& a : partial_alias) { + // 前方一致したら覚えておく + if (starts_with_ignorecase(a.first, name)) { + found.push_back(a); + } } // 見付からない場合はエラー @@ -549,16 +1069,15 @@ MainApp::SetLogopt1(const std::string& i // 1つだけなら確定 if (found.size() == 1) { - Object *obj = found[0]; - obj->loglevel = level; + found[0].second->SetLogLevel(level); return true; } // 複数あれば候補文字列を作成 *errmsg = string_format("Ambiguous logname \"%s\": candidates are", name.c_str()); - for (const auto& cand : candidates) { - *errmsg += string_format(" \"%s\"", cand.c_str()); + for (const auto& cand : found) { + *errmsg += string_format(" \"%s\"", cand.first.c_str()); } return false; } @@ -571,7 +1090,7 @@ MainApp::GetLogNames() std::vector sortobj; // エイリアスを持つオブジェクトだけ抜き出す - for (const auto& obj : gObjects) { + for (const auto& obj : gMainApp.GetObjects()) { if (obj->GetAliases().empty() == false) { sortobj.emplace_back(obj); } @@ -579,10 +1098,10 @@ MainApp::GetLogNames() // aliases の一語目で sortobj をソートする std::sort(sortobj.begin(), sortobj.end(), - [](const auto& a, const auto *b) { + [](const auto a, const auto b) { const auto& sa = a->GetAliases()[0]; const auto& sb = b->GetAliases()[0]; - return strcmp(sa.c_str(), sb.c_str()) < 0; + return sa < sb; } ); @@ -591,7 +1110,7 @@ MainApp::GetLogNames() const auto& aliases = obj->GetAliases(); std::string str; - str += aliases[0]; + str = aliases[0]; if (aliases.size() > 1) { str += " (alias:"; for (int i = 1, sz = aliases.size(); i < sz; i++) { @@ -612,24 +1131,23 @@ MainApp::GetLogNames() // ファイルが存在すればその時点で戻り値とする。ファイルが存在したけど // 何らか不都合があったとしても (例えばファイルサイズが 0 だとか // パーミッションが足りないとか) 次を試すとかはしない。 -// 2. name が '~' から始まっていればホームディレクトリに展開。 -// 3. name が相対パスなら VM ディレクトリからの相対パスとする。 -// 4. name は絶対パスのはずなので、そのまま使用する。 +// 2. 通常手順(相対パスは VM ディレクトリ起点) でパスを展開。 +// ファイルの有無は不問。 std::string MainApp::SearchFile(const std::string& name) const { - std::string path; - struct stat st; - // 1. パス区切りを含んでなければ、ファイル名のみ if (name.find('/') == std::string::npos) { + std::string path; + struct stat st; + // 1a. VM ディレクトリ path = GetVMDir() + name; if (stat(path.c_str(), &st) == 0) { return path; } - // 2. 親ディレクトリ + // 1b. 親ディレクトリ path = GetVMDir() + "../" + name; if (stat(path.c_str(), &st) == 0) { return path; @@ -639,38 +1157,134 @@ MainApp::SearchFile(const std::string& n return ""; } - // 2. 先頭の '~' を $HOME に展開 - path = name; - if (path[0] == '~' && path[1] == '/') { - const char *home = getenv("HOME"); - if (home == NULL) { - home = ""; - } - path = string_format("%s%s", home, path.c_str() + 1); + // 2. 通常手順でパスを展開。 + return NormalizePath(name); +} + +// path を必要なら絶対パスに展開して返す。 +// 相対パスは VM ディレクトリを起点とする。 +std::string +MainApp::NormalizePath(const std::string& path) const +{ + return NormalizePath(path, GetVMDir()); +} + +// path を必要なら絶対パスに展開して返す。 +// 相対パスは basedir を起点とする。 +/*static*/ std::string +MainApp::NormalizePath(const std::string& path, const std::string& basedir) +{ + // 絶対パスならそのまま返す。 + if (path[0] == '/') { + return path; } - // 3. 相対パスなら、VM ディレクトリからの相対 - if (path[0] != '/') { - path = GetVMDir() + path; + std::string newpath; + + if (path[0] == '~') { + // 先頭が '~' ならホームディレクトリに展開。 + + struct passwd *passwd = getpwuid(getuid()); + // 見付からないことはないはずだが、一応。 + if (passwd != NULL) { + newpath = std::string(passwd->pw_dir); + } + newpath += &path[1]; + } else { + // basedir からの相対パスとして展開。 + + newpath = basedir; + if (newpath.empty() || newpath.back() != '/') { + newpath += '/'; + } + newpath += path; } - return path; + return newpath; } -// 現在の VM が指定の feature を持っているか? +// 現在の VM が指定のケーパビリティを持っているか? bool -MainApp::HasVMFeature(vmf_t feature) const +MainApp::Has(VMCap cap) const +{ + uint32 vmcap = 1U << (int)GetVMType(); + return (vmcap & (uint32)cap) != 0; +} + +// VM 機種名を返す。 +const char * +MainApp::GetVMName() const { - uint32 vmf = 1 << GetVMType(); - return (vmf & feature) != 0; + if ((bool)pVM) { + return pVM->GetVMName(); + } else { + return NULL; // not configured + } } // コンパイルされている host netdriver の一覧を表示。 void MainApp::ShowHostnet() const { - auto list = NetDriverSelector::GetDrivers(); + auto list = HostNetDevice::GetDrivers(); for (const auto& name : list) { printf(" %s\n", name.c_str()); } } + +// オブジェクト登録 +void +MainApp::RegistObject(Object *obj) +{ + // ID が重複していないかチェック (NONE なら重複可) + auto id = obj->GetId(); + if (id != OBJ_NONE) { + if (FindObject(id)) { + PANIC("%s already exists", Object::GetIdStr(id)); + } + } + + obj->logger = gMainApp.GetLogger(); + + objects.push_back(obj); +} + +// オブジェクト削除 +void +MainApp::UnregistObject(Object *obj) +{ + // 最初に見付かった一つを削除するだけでいい + for (auto it = objects.begin(); it != objects.end(); ++it) { + if (*it == obj) { + objects.erase(it); + break; + } + } +} + +// 指定された id を持つオブジェクトを返す。なければ NULL を返す。 +Object * +MainApp::FindObject(uint id) const +{ + // NONE はここで検索しない (SCSI デバイスなど、そっちで検索する) + if (id == OBJ_NONE) { + return NULL; + } + for (auto obj : objects) { + if (obj->GetId() == id) { + return obj; + } + } + return NULL; +} + +// 指定された id を持つオブジェクトを探す。なければ assert する。 +Object * +MainApp::GetObject(uint id) const +{ + Object *obj = FindObject(id); + if (__predict_false(obj == NULL)) { + PANIC("objid=%s not found", Object::GetIdStr(id)); + } + return obj; +}