1 | /* $Id: $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxMemBalloon - Memory balloon notification.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 | #define _WIN32_WINNT 0x0500
|
---|
22 | #include <windows.h>
|
---|
23 | #include <psapi.h>
|
---|
24 | #include "VBoxTray.h"
|
---|
25 | #include "VBoxMemBalloon.h"
|
---|
26 | #include <VBoxDisplay.h>
|
---|
27 | #include <VBox/VMMDev.h>
|
---|
28 | #include <VBoxGuestInternal.h>
|
---|
29 | #include <iprt/assert.h>
|
---|
30 | #include "helpers.h"
|
---|
31 | #include <winternl.h>
|
---|
32 |
|
---|
33 | typedef struct _VBOXMEMBALLOONCONTEXT
|
---|
34 | {
|
---|
35 | const VBOXSERVICEENV *pEnv;
|
---|
36 | uint32_t uMemBalloonSize;
|
---|
37 | } VBOXMEMBALLOONCONTEXT;
|
---|
38 |
|
---|
39 |
|
---|
40 | static VBOXMEMBALLOONCONTEXT gCtx = {0};
|
---|
41 |
|
---|
42 |
|
---|
43 | int VBoxMemBalloonInit(const VBOXSERVICEENV *pEnv, void **ppInstance, bool *pfStartThread)
|
---|
44 | {
|
---|
45 | HANDLE gVBoxDriver = pEnv->hDriver;
|
---|
46 | DWORD cbReturned;
|
---|
47 |
|
---|
48 | Log(("VBoxMemBalloonInit: Init\n"));
|
---|
49 |
|
---|
50 | gCtx.pEnv = pEnv;
|
---|
51 | gCtx.uMemBalloonSize = 0;
|
---|
52 |
|
---|
53 | /* Check balloon size */
|
---|
54 | DWORD dwMemBalloonSize;
|
---|
55 | if (DeviceIoControl(gVBoxDriver, VBOXGUEST_IOCTL_CTL_CHECK_BALLOON_MASK, NULL, 0, &dwMemBalloonSize, sizeof(dwMemBalloonSize), &cbReturned, NULL))
|
---|
56 | {
|
---|
57 | Log(("VBoxMemBalloonInit: new balloon size %d MB\n", dwMemBalloonSize));
|
---|
58 | gCtx.uMemBalloonSize = dwMemBalloonSize;
|
---|
59 | }
|
---|
60 | else
|
---|
61 | Log(("VBoxMemBalloonInit: DeviceIoControl (balloon) failed with %d\n", GetLastError()));
|
---|
62 |
|
---|
63 | *pfStartThread = true;
|
---|
64 | *ppInstance = &gCtx;
|
---|
65 | return VINF_SUCCESS;
|
---|
66 | }
|
---|
67 |
|
---|
68 |
|
---|
69 | void VBoxMemBalloonDestroy(const VBOXSERVICEENV *pEnv, void *pInstance)
|
---|
70 | {
|
---|
71 | Log(("VBoxMemBalloonDestroy\n"));
|
---|
72 | return;
|
---|
73 | }
|
---|
74 |
|
---|
75 | uint32_t VBoxMemBalloonQuerySize()
|
---|
76 | {
|
---|
77 | return gCtx.uMemBalloonSize;
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Thread function to wait for and process seamless mode change
|
---|
82 | * requests
|
---|
83 | */
|
---|
84 | unsigned __stdcall VBoxMemBalloonThread(void *pInstance)
|
---|
85 | {
|
---|
86 | VBOXMEMBALLOONCONTEXT *pCtx = (VBOXMEMBALLOONCONTEXT *)pInstance;
|
---|
87 | HANDLE gVBoxDriver = pCtx->pEnv->hDriver;
|
---|
88 | bool fTerminate = false;
|
---|
89 | VBoxGuestFilterMaskInfo maskInfo;
|
---|
90 | DWORD cbReturned;
|
---|
91 |
|
---|
92 | maskInfo.u32OrMask = VMMDEV_EVENT_BALLOON_CHANGE_REQUEST;
|
---|
93 | maskInfo.u32NotMask = 0;
|
---|
94 | if (DeviceIoControl (gVBoxDriver, VBOXGUEST_IOCTL_CTL_FILTER_MASK, &maskInfo, sizeof (maskInfo), NULL, 0, &cbReturned, NULL))
|
---|
95 | {
|
---|
96 | Log(("VBoxMemBalloonThread: DeviceIOControl(CtlMask - or) succeeded\n"));
|
---|
97 | }
|
---|
98 | else
|
---|
99 | {
|
---|
100 | Log(("VBoxMemBalloonThread: DeviceIOControl(CtlMask) failed, SeamlessChangeThread exited\n"));
|
---|
101 | return 0;
|
---|
102 | }
|
---|
103 |
|
---|
104 | do
|
---|
105 | {
|
---|
106 | /* wait for a seamless change event */
|
---|
107 | VBoxGuestWaitEventInfo waitEvent;
|
---|
108 | waitEvent.u32TimeoutIn = 5000;
|
---|
109 | waitEvent.u32EventMaskIn = VMMDEV_EVENT_BALLOON_CHANGE_REQUEST;
|
---|
110 | if (DeviceIoControl(gVBoxDriver, VBOXGUEST_IOCTL_WAITEVENT, &waitEvent, sizeof(waitEvent), &waitEvent, sizeof(waitEvent), &cbReturned, NULL))
|
---|
111 | {
|
---|
112 | Log(("VBoxMemBalloonThread: DeviceIOControl succeded\n"));
|
---|
113 |
|
---|
114 | /* are we supposed to stop? */
|
---|
115 | if (WaitForSingleObject(pCtx->pEnv->hStopEvent, 0) == WAIT_OBJECT_0)
|
---|
116 | break;
|
---|
117 |
|
---|
118 | Log(("VBoxMemBalloonThread: checking event\n"));
|
---|
119 |
|
---|
120 | /* did we get the right event? */
|
---|
121 | if (waitEvent.u32EventFlagsOut & VMMDEV_EVENT_BALLOON_CHANGE_REQUEST)
|
---|
122 | {
|
---|
123 | DWORD dwMemBalloonSize;
|
---|
124 | if (DeviceIoControl(gVBoxDriver, VBOXGUEST_IOCTL_CTL_CHECK_BALLOON_MASK, NULL, 0, &dwMemBalloonSize, sizeof(dwMemBalloonSize), &cbReturned, NULL))
|
---|
125 | {
|
---|
126 | Log(("VBoxMemBalloonThread: new balloon size % MB\n", dwMemBalloonSize));
|
---|
127 | pCtx->uMemBalloonSize = dwMemBalloonSize;
|
---|
128 | }
|
---|
129 | else
|
---|
130 | Log(("VBoxMemBalloonThread: DeviceIoControl (balloon) failed with %d\n", GetLastError()));
|
---|
131 | }
|
---|
132 | }
|
---|
133 | else
|
---|
134 | {
|
---|
135 | Log(("VBoxMemBalloonThread: error 0 from DeviceIoControl VBOXGUEST_IOCTL_WAITEVENT\n"));
|
---|
136 |
|
---|
137 | /* sleep a bit to not eat too much CPU in case the above call always fails */
|
---|
138 | if (WaitForSingleObject(pCtx->pEnv->hStopEvent, 10) == WAIT_OBJECT_0)
|
---|
139 | {
|
---|
140 | fTerminate = true;
|
---|
141 | break;
|
---|
142 | }
|
---|
143 | }
|
---|
144 | }
|
---|
145 | while (!fTerminate);
|
---|
146 |
|
---|
147 | maskInfo.u32OrMask = 0;
|
---|
148 | maskInfo.u32NotMask = VMMDEV_EVENT_BALLOON_CHANGE_REQUEST;
|
---|
149 | if (DeviceIoControl (gVBoxDriver, VBOXGUEST_IOCTL_CTL_FILTER_MASK, &maskInfo, sizeof (maskInfo), NULL, 0, &cbReturned, NULL))
|
---|
150 | {
|
---|
151 | Log(("VBoxMemBalloonThread: DeviceIOControl(CtlMask - not) succeeded\n"));
|
---|
152 | }
|
---|
153 | else
|
---|
154 | {
|
---|
155 | Log(("VBoxMemBalloonThread: DeviceIOControl(CtlMask) failed\n"));
|
---|
156 | }
|
---|
157 |
|
---|
158 | Log(("VBoxMemBalloonThread: finished mem balloon change request thread\n"));
|
---|
159 | return 0;
|
---|
160 | }
|
---|
161 |
|
---|
162 |
|
---|