Skip to contentSkip to conversation

kvm: kvmctl: create secondary vcpus

kvm-commits Jun Koi 2 participants 2 messages Jun 5, 2007 3 messages not in the archive KVM

lore
Share & tools
lore

Jun Koi

author
#2 lore raw
In reply to a message not in the archive

So now we can have SMP guest with KVM? Great!

One thing I dont understand: I dont see where you unlock vcpu[n].sipi_sem?

Thanks,
Jun

Quoted 130 lines · Avi Kivity

repository: /home/avi/kvm
branch: master
commit 61f2f77a6f11a08f06134c03e083713c78765509

Author: Avi Kivity <avi-atKUWr5tajBWk0Htik3J/[email protected]>
Date:   Mon Jun 4 19:04:14 2007 +0300

kvm: kvmctl: create secondary vcpus

user/Makefile +2 -0
index dc5b5a6..6050bf9 100644
@@ -32,6 +32,8 @@ endif
 
 all: kvmctl libkvm.a flatfiles
 
+kvmctl: LDFLAGS += -pthread -lrt
+
 kvmctl: kvmctl.o main.o
 
 balloon_ctl: balloon_ctl.o
user/main.c +64 -2
index 41fc288..2e0a41f 100644
@@ -21,9 +21,33 @@
 #include <fcntl.h>
 #include <stdlib.h>
 #include <string.h>
+#include <semaphore.h>
+#include <sys/types.h>
+#include <errno.h>
+#include <pthread.h>
+#include <sys/syscall.h>
+#include <linux/unistd.h>
+
+
+static int gettid(void)
+{
+    return syscall(__NR_gettid);
+}
 
 kvm_context_t kvm;
 
+#define MAX_VCPUS 4
+
+static int ncpus = 1;
+static sem_t init_sem;
+
+struct vcpu_info {
+    pid_t tid;
+    sem_t sipi_sem;
+};
+
+struct vcpu_info *vcpus;
+
 static int test_inb(void *opaque, uint16_t addr, uint8_t *value)
 {
     printf("inb 0x%x\n", addr);
@@ -171,6 +195,33 @@ static void enter_32(kvm_context_t kvm)
     kvm_set_sregs(kvm, 0, &sregs);
 }
 
+static void init_vcpu(int n)
+{
+    vcpus[n].tid = gettid();
+    sem_post(&init_sem);
+}
+
+static void *do_create_vcpu(void *_n)
+{
+    int n = (long)_n;
+
+    kvm_create_vcpu(kvm, n);
+    init_vcpu(n);
+    printf("vcpu %d: waiting for sipi\n", n);
+    sem_wait(&vcpus[n].sipi_sem);
+    printf("vcpu %d: running\n", n);
+    kvm_run(kvm, n);
+    return NULL;
+}
+
+static void start_vcpu(int n)
+{
+    pthread_t thread;
+
+    sem_init(&vcpus[n].sipi_sem, 0, 0);
+    pthread_create(&thread, NULL, do_create_vcpu, (void *)(long)n);
+}
+
 const char *progname;
 
 static void usage()
@@ -191,7 +242,7 @@ static int isarg(const char *arg, const char *longform, const char *shortform)
 int main(int ac, char **av)
 {
        void *vm_mem;
-       int ncpus = 1;
+       int i;
 
        progname = av[0];
        while (ac > 1 && av[1][0] =='-') {
@@ -207,6 +258,12 @@ int main(int ac, char **av)
            ++av, --ac;
        }
 
+       vcpus = calloc(ncpus, sizeof *vcpus);
+       if (!vcpus) {
+           fprintf(stderr, "calloc failed\n");
+           return 1;
+       }
+
        kvm = kvm_init(&test_callbacks, 0);
        if (!kvm) {
            fprintf(stderr, "kvm_init failed\n");
@@ -226,6 +283,12 @@ int main(int ac, char **av)
        if (ac > 2)
            load_file(vm_mem + 0x100000, av[2]);
 
+       sem_init(&init_sem, 0, 1 - ncpus);
+       init_vcpu(0);
+       for (i = 1; i < ncpus; ++i)
+           start_vcpu(i);
+       sem_wait(&init_sem);
+
        kvm_run(kvm, 0);
 
        return 0;
 
-------------------------------------------------------------------------
Quoted 9 lines

This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
kvm-commits mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/kvm-commits

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/

#4 lore raw
In reply to a message not in the archive
Quoted 2 lines · Jun Koi

So now we can have SMP guest with KVM? Great!

It's not done yet.

Quoted 3 lines

One thing I dont understand: I dont see where you unlock vcpu[n].sipi_sem?

That's still cooking. I've written small pseudo apic which can issue the sipi.

Signature · 8 lines
error compiling committee.c: too many arguments to function


-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/