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