From 4c5a1e4db7c68b0e7edf5687dc10beeb776bad9f Mon Sep 17 00:00:00 2001 From: Lai Jiangshan Date: Mon, 7 Mar 2011 17:05:04 +0800 Subject: QMP: QError: New QERR_UNSUPPORTED New QERR_UNSUPPORTED for unsupported commands or requests. Signed-off-by: Luiz Capitulino --- qerror.c | 4 ++++ qerror.h | 3 +++ 2 files changed, 7 insertions(+) diff --git a/qerror.c b/qerror.c index 485560418b..4f3b7cacb1 100644 --- a/qerror.c +++ b/qerror.c @@ -200,6 +200,10 @@ static const QErrorStringTable qerror_table[] = { .error_fmt = QERR_UNDEFINED_ERROR, .desc = "An undefined error has ocurred", }, + { + .error_fmt = QERR_UNSUPPORTED, + .desc = "this feature or command is not currently supported", + }, { .error_fmt = QERR_UNKNOWN_BLOCK_FORMAT_FEATURE, .desc = "'%(device)' uses a %(format) feature which is not " diff --git a/qerror.h b/qerror.h index df61d2c2c6..582b5efe45 100644 --- a/qerror.h +++ b/qerror.h @@ -165,6 +165,9 @@ QError *qobject_to_qerror(const QObject *obj); #define QERR_UNDEFINED_ERROR \ "{ 'class': 'UndefinedError', 'data': {} }" +#define QERR_UNSUPPORTED \ + "{ 'class': 'Unsupported', 'data': {} }" + #define QERR_UNKNOWN_BLOCK_FORMAT_FEATURE \ "{ 'class': 'UnknownBlockFormatFeature', 'data': { 'device': %s, 'format': %s, 'feature': %s } }" -- cgit v1.2.1 From a404666457b54142d6cfe8302bcded9be4cda379 Mon Sep 17 00:00:00 2001 From: Lai Jiangshan Date: Mon, 7 Mar 2011 17:05:15 +0800 Subject: QMP: add inject-nmi qmp command inject-nmi command injects an NMI on all CPUs of guest. It is only supported for x86 guest currently, it will returns "Unsupported" error for non-x86 guest. Signed-off-by: Luiz Capitulino --- monitor.c | 17 +++++++++++++++++ qmp-commands.hx | 27 +++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/monitor.c b/monitor.c index f63cce050f..81d3c9b748 100644 --- a/monitor.c +++ b/monitor.c @@ -2555,6 +2555,23 @@ static void do_inject_nmi(Monitor *mon, const QDict *qdict) break; } } + +static int do_inject_nmi_all(Monitor *mon, const QDict *qdict, QObject **ret_data) +{ + CPUState *env; + + for (env = first_cpu; env != NULL; env = env->next_cpu) { + cpu_interrupt(env, CPU_INTERRUPT_NMI); + } + + return 0; +} +#else +static int do_inject_nmi_all(Monitor *mon, const QDict *qdict, QObject **ret_data) +{ + qerror_report(QERR_UNSUPPORTED); + return -1; +} #endif static void do_info_status_print(Monitor *mon, const QObject *data) diff --git a/qmp-commands.hx b/qmp-commands.hx index a9f109a391..ae08b7a59d 100644 --- a/qmp-commands.hx +++ b/qmp-commands.hx @@ -427,6 +427,33 @@ Example: "filename": "/tmp/physical-mem-dump" } } <- { "return": {} } +EQMP + + { + .name = "inject-nmi", + .args_type = "", + .params = "", + .help = "", + .user_print = monitor_user_noop, + .mhandler.cmd_new = do_inject_nmi_all, + }, + +SQMP +inject-nmi +---------- + +Inject an NMI on guest's CPUs. + +Arguments: None. + +Example: + +-> { "execute": "inject-nmi" } +<- { "return": {} } + +Note: inject-nmi is only supported for x86 guest currently, it will + returns "Unsupported" error for non-x86 guest. + EQMP { -- cgit v1.2.1 From e9b4b432e781863869b074d46966bfa0bd672b8f Mon Sep 17 00:00:00 2001 From: Luiz Capitulino Date: Fri, 29 Apr 2011 12:11:50 -0300 Subject: HMP: Use QMP inject nmi implementation This **CHANGES** the human monitor "nmi" command behavior. Currently it accepts an CPU argument which, when provided, will send the NMI to the specified CPU. This feature is of discussable value though and HMP shouldn't have more features than QMP, so let's use QMP's instead (it's also simpler). Signed-off-by: Luiz Capitulino --- hmp-commands.hx | 9 +++++---- monitor.c | 16 ++-------------- qmp-commands.hx | 2 +- 3 files changed, 8 insertions(+), 19 deletions(-) diff --git a/hmp-commands.hx b/hmp-commands.hx index 834e6a8c87..6ad8806785 100644 --- a/hmp-commands.hx +++ b/hmp-commands.hx @@ -740,10 +740,11 @@ ETEXI #if defined(TARGET_I386) { .name = "nmi", - .args_type = "cpu_index:i", - .params = "cpu", - .help = "inject an NMI on the given CPU", - .mhandler.cmd = do_inject_nmi, + .args_type = "", + .params = "", + .help = "inject an NMI on all guest's CPUs", + .user_print = monitor_user_noop, + .mhandler.cmd_new = do_inject_nmi, }, #endif STEXI diff --git a/monitor.c b/monitor.c index 81d3c9b748..6af6a4d999 100644 --- a/monitor.c +++ b/monitor.c @@ -2544,19 +2544,7 @@ static void do_wav_capture(Monitor *mon, const QDict *qdict) #endif #if defined(TARGET_I386) -static void do_inject_nmi(Monitor *mon, const QDict *qdict) -{ - CPUState *env; - int cpu_index = qdict_get_int(qdict, "cpu_index"); - - for (env = first_cpu; env != NULL; env = env->next_cpu) - if (env->cpu_index == cpu_index) { - cpu_interrupt(env, CPU_INTERRUPT_NMI); - break; - } -} - -static int do_inject_nmi_all(Monitor *mon, const QDict *qdict, QObject **ret_data) +static int do_inject_nmi(Monitor *mon, const QDict *qdict, QObject **ret_data) { CPUState *env; @@ -2567,7 +2555,7 @@ static int do_inject_nmi_all(Monitor *mon, const QDict *qdict, QObject **ret_dat return 0; } #else -static int do_inject_nmi_all(Monitor *mon, const QDict *qdict, QObject **ret_data) +static int do_inject_nmi(Monitor *mon, const QDict *qdict, QObject **ret_data) { qerror_report(QERR_UNSUPPORTED); return -1; diff --git a/qmp-commands.hx b/qmp-commands.hx index ae08b7a59d..92c5c3a318 100644 --- a/qmp-commands.hx +++ b/qmp-commands.hx @@ -435,7 +435,7 @@ EQMP .params = "", .help = "", .user_print = monitor_user_noop, - .mhandler.cmd_new = do_inject_nmi_all, + .mhandler.cmd_new = do_inject_nmi, }, SQMP -- cgit v1.2.1 From 91b8eddf41fd62ecbc61cb901c40f50287126fac Mon Sep 17 00:00:00 2001 From: Stefan Hajnoczi Date: Wed, 25 May 2011 19:48:00 +0100 Subject: QMP: add get_events(wait=True) option The get_events() function polls for new QMP events and then returns. It can be useful to wait for the next QMP event so add the boolean 'wait' keyword argument. Signed-off-by: Stefan Hajnoczi Signed-off-by: Luiz Capitulino --- QMP/qmp.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/QMP/qmp.py b/QMP/qmp.py index 14ce8b0d05..25655088fb 100644 --- a/QMP/qmp.py +++ b/QMP/qmp.py @@ -43,7 +43,7 @@ class QEMUMonitorProtocol: family = socket.AF_UNIX return socket.socket(family, socket.SOCK_STREAM) - def __json_read(self): + def __json_read(self, only_event=False): while True: data = self.__sockfile.readline() if not data: @@ -51,7 +51,8 @@ class QEMUMonitorProtocol: resp = json.loads(data) if 'event' in resp: self.__events.append(resp) - continue + if not only_event: + continue return resp error = socket.error @@ -106,9 +107,11 @@ class QEMUMonitorProtocol: qmp_cmd['id'] = id return self.cmd_obj(qmp_cmd) - def get_events(self): + def get_events(self, wait=False): """ Get a list of available QMP events. + + @param wait: block until an event is available (bool) """ self.__sock.setblocking(0) try: @@ -118,6 +121,8 @@ class QEMUMonitorProtocol: # No data available pass self.__sock.setblocking(1) + if not self.__events and wait: + self.__json_read(only_event=True) return self.__events def clear_events(self): -- cgit v1.2.1 From 37628f11c6b190ebb3082938868aadb23942ee3a Mon Sep 17 00:00:00 2001 From: Stefan Hajnoczi Date: Wed, 25 May 2011 19:48:01 +0100 Subject: QMP: add server mode to QEMUMonitorProtocol QEMU supports socket chardevs that establish connections like a server or a client. The QEMUMonitorProtocol class only supports connecting as a client. It is not possible to connect race-free when launching QEMU since trying to connect before QEMU has bound and is listening on the socket results in failure. Add the QEMUMonitorProtocol(server=True) argument to bind and listen on the socket. The QEMU process can then be launched and connects to the already existing QMP socket without a race condition: qmp = qmp.QEMUMonitorProtocol(monitor_path, server=True) popen = subprocess.Popen(args) qmp.accept() Signed-off-by: Stefan Hajnoczi Signed-off-by: Luiz Capitulino --- QMP/qmp.py | 43 ++++++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/QMP/qmp.py b/QMP/qmp.py index 25655088fb..c7dbea076d 100644 --- a/QMP/qmp.py +++ b/QMP/qmp.py @@ -22,19 +22,24 @@ class QMPCapabilitiesError(QMPError): pass class QEMUMonitorProtocol: - def __init__(self, address): + def __init__(self, address, server=False): """ Create a QEMUMonitorProtocol class. @param address: QEMU address, can be either a unix socket path (string) or a tuple in the form ( address, port ) for a TCP connection - @note No connection is established, this is done by the connect() method + @param server: server mode listens on the socket (bool) + @raise socket.error on socket connection errors + @note No connection is established, this is done by the connect() or + accept() methods """ self.__events = [] self.__address = address self.__sock = self.__get_sock() - self.__sockfile = self.__sock.makefile() + if server: + self.__sock.bind(self.__address) + self.__sock.listen(1) def __get_sock(self): if isinstance(self.__address, tuple): @@ -43,6 +48,17 @@ class QEMUMonitorProtocol: family = socket.AF_UNIX return socket.socket(family, socket.SOCK_STREAM) + def __negotiate_capabilities(self): + self.__sockfile = self.__sock.makefile() + greeting = self.__json_read() + if greeting is None or not greeting.has_key('QMP'): + raise QMPConnectError + # Greeting seems ok, negotiate capabilities + resp = self.cmd('qmp_capabilities') + if "return" in resp: + return greeting + raise QMPCapabilitiesError + def __json_read(self, only_event=False): while True: data = self.__sockfile.readline() @@ -67,14 +83,19 @@ class QEMUMonitorProtocol: @raise QMPCapabilitiesError if fails to negotiate capabilities """ self.__sock.connect(self.__address) - greeting = self.__json_read() - if greeting is None or not greeting.has_key('QMP'): - raise QMPConnectError - # Greeting seems ok, negotiate capabilities - resp = self.cmd('qmp_capabilities') - if "return" in resp: - return greeting - raise QMPCapabilitiesError + return self.__negotiate_capabilities() + + def accept(self): + """ + Await connection from QMP Monitor and perform capabilities negotiation. + + @return QMP greeting dict + @raise socket.error on socket connection errors + @raise QMPConnectError if the greeting is not received + @raise QMPCapabilitiesError if fails to negotiate capabilities + """ + self.__sock, _ = self.__sock.accept() + return self.__negotiate_capabilities() def cmd_obj(self, qmp_cmd): """ -- cgit v1.2.1