Index: Kernel/Core/ADC/X86/APM.cs
===================================================================
--- Kernel/Core/ADC/X86/APM.cs	(revision 0)
+++ Kernel/Core/ADC/X86/APM.cs	(revision 0)
@@ -0,0 +1,144 @@
+//
+// (C) 2006-2008 The SharpOS Project Team (http://www.sharpos.org)
+//
+// Authors:
+//	Shevchenko Taras aka airstep <back.neomind@gmail.com>
+//
+// Licensed under the terms of the GNU GPL v3,
+//  with Classpath Linking Exception for Libraries
+//
+
+using System;
+using AOTAttr = SharpOS.AOT.Attributes;
+using SharpOS.AOT.X86;
+
+namespace SharpOS.Kernel.ADC.X86
+{
+	public unsafe class APM
+	{
+		public static bool IsInstalled ()
+		{
+			//UInt32 PMStack = SwitchToRealMode ();
+
+			//Asm.MOV (R16.AX, 0x5300);
+			//Asm.XOR (R16.BX, R16.BX);
+			//Asm.XOR (R16.CX, R16.CX);
+			//Asm.INT (0x15);
+
+			//SwitchToProtectedMode (PMStack);
+
+			return true;
+		}
+
+        public static void PowerOff()
+		{
+			UInt32 PMStack = SwitchToRealMode ();
+
+			//SwitchToProtectedMode (PMStack);
+		}
+
+	    private static uint SwitchToRealMode ()
+		{
+			UInt32 CR0_PE_OFF = 0xfffffffe;
+            UInt32 GRUB_MEMORY_MACHINE_REAL_STACK = 0x1FF0; //(0x2000 - 0x10)
+            UInt16 GRUB_MEMORY_MACHINE_PSEUDO_REAL_DSEG = 0x20;
+            //ushort GRUB_MEMORY_MACHINE_PSEUDO_REAL_CSEG = 0x18;
+            UInt32 PMStack = 0x8000;
+
+            // just in case, set GDT
+			Asm.LGDT (new SharpOS.AOT.X86.Memory ("GDTPointer"));
+
+            // save the protected mode stack
+			Asm.MOV (R32.EAX, R32.ESP);
+			Asm.MOV (&PMStack, R32.EAX);
+
+            // get the return address
+			Asm.MOV (R32.EAX, R32.ESP);
+            Asm.MOV (&GRUB_MEMORY_MACHINE_REAL_STACK, R32.EAX);
+
+            // set up new stack
+            Asm.MOV (R32.EAX, GRUB_MEMORY_MACHINE_REAL_STACK);
+			Asm.MOV (R32.ESP, R32.EAX);
+			Asm.MOV (R32.EBP, R32.EAX);
+
+            // set up segment limits
+            //Asm.MOV(R16.AX, GRUB_MEMORY_MACHINE_PSEUDO_REAL_DSEG);
+            //Asm.MOV(Seg.DS, R16.AX);
+            //Asm.MOV(Seg.ES, R16.AX);
+            //Asm.MOV(Seg.FS, R16.AX);
+            //Asm.MOV(Seg.GS, R16.AX);
+            //Asm.MOV(Seg.SS, R16.AX);
+
+
+            // this might be an extra step
+            // jump to a 16 bit segment
+            //Asm.JMP("tmpcseg");
+
+            //Asm.LABEL("tmpcseg");
+
+            ADC.TextMode.WriteLine("Switch to real mode.");
+
+
+            Asm.MOV (R32.EAX, CR.CR0);
+            Asm.AND (R32.EAX, CR0_PE_OFF);
+            Asm.MOV (CR.CR0, R32.EAX);
+
+            // poweroff after switch to real mode
+            // but we need manualy power off system
+
+            //Asm.JMP (0, "realcseg");
+
+            //Asm.LABEL ("realcseg");
+            //Asm.XOR (R32.EAX, R32.EAX);
+            //Asm.MOV (Seg.DS, R16.AX);
+            //Asm.MOV (Seg.ES, R16.AX);
+            //Asm.MOV (Seg.FS, R16.AX);
+            //Asm.MOV (Seg.GS, R16.AX);
+            //Asm.MOV (Seg.SS, R16.AX);
+
+            //Asm.STI ();
+
+            //Asm.RET ();
+
+			return PMStack;
+		}
+
+		private static void SwitchToProtectedMode (uint PMStack)
+		{
+			UInt32 CR0_PE_ON = 0x1;
+			//ushort PROT_MODE_CSEG = 0x8; 
+			UInt16 PROT_MODE_DSEG = 0x10;
+			UInt32 STACKOFF = 0x1FF0; //(0x2000 - 0x10)
+
+			Asm.LGDT (new SharpOS.AOT.X86.Memory ("GDTPointer"));
+
+			Asm.MOV (R32.EAX, CR.CR0);
+			Asm.OR (R32.EAX, CR0_PE_ON);
+			Asm.MOV (CR.CR0, R32.EAX);
+
+			Asm.JMP (0x8, "protcseg");
+
+			Asm.LABEL ("protcseg");
+			Asm.MOV (R16.AX, PROT_MODE_DSEG);
+			Asm.MOV (Seg.DS, R16.AX);
+			Asm.MOV (Seg.ES, R16.AX);
+			Asm.MOV (Seg.FS, R16.AX);
+			Asm.MOV (Seg.GS, R16.AX);
+			Asm.MOV (Seg.SS, R16.AX);
+
+			Asm.MOV (R32.EAX, R32.ESP);
+			Asm.MOV (&STACKOFF, R32.EAX);
+
+			Asm.MOV (R32.EAX, PMStack);
+			Asm.MOV (R32.ESP, R32.EAX);
+			Asm.MOV (R32.EBP, R32.EAX);
+
+			Asm.MOV (R32.EAX, STACKOFF);
+			Asm.MOV (R32.ESP, R32.EAX);
+			Asm.XOR (R32.EAX, R32.EAX);
+
+			Asm.RET ();
+		}
+
+	}
+}
Index: Kernel/Core/ADC/X86/BootControl.cs
===================================================================
--- Kernel/Core/ADC/X86/BootControl.cs	(revision 1230)
+++ Kernel/Core/ADC/X86/BootControl.cs	(working copy)
@@ -30,12 +30,12 @@
 		/// </summary>
 		public static void PowerOff ()
 		{
-			// TODO: turn off system here..
-
 			// Disable interrupts
-			Asm.CLI ();
+			Asm.CLI();
+            
+			APM.PowerOff();
 
-			// Halt the system
+ 			// Halt the system
 			Asm.HLT ();
 		}
 
@@ -75,8 +75,8 @@
 		{
 			// Disable interrupts
 			Asm.CLI ();
-
-			// Clear all keyboard buffers (output and command buffers)
+           
+            // Clear all keyboard buffers (output and command buffers)
 			byte temp;
 			do {
 				temp = IO.ReadByte (IO.Port.KB_controller_commands);
Index: Kernel/Core/EntryModule.cs
===================================================================
--- Kernel/Core/EntryModule.cs	(revision 1230)
+++ Kernel/Core/EntryModule.cs	(working copy)
@@ -73,9 +73,14 @@
 		/// </param>
 		/// <param name="kernelEnd">
 		/// The address of the last byte reserved for the kernel.
-		/// </param>
+		/// 		[SharpOS.AOT.Attributes.KernelMain]
+        public unsafe static void BootEntry(uint magic, uint pointer, uint kernelStart, uint kernelEnd)
+		{
+		}
+
+	    /// </param>
 		[SharpOS.AOT.Attributes.KernelMain]
-		public unsafe static void BootEntry (uint magic, uint pointer, uint kernelStart, uint kernelEnd)
+		public unsafe static void BootEntry1 (uint magic, uint pointer, uint kernelStart, uint kernelEnd)
 		{
 			// Initialize architecture-specific portion of the kernel
 			Architecture.Setup ();
@@ -294,6 +299,15 @@
 			BootControl.Reboot ();
 		}
 
+        /// <summary>
+		/// Performs poweroff
+		/// function.
+		/// </summary>
+		public unsafe static void PowerOff()
+		{
+			SetKernelStage(KernelStage.Stop);
+			BootControl.PowerOff();
+		}
 		#endregion
 	}
 }
Index: Kernel/Core/SharpOS.Kernel.Core.csproj
===================================================================
--- Kernel/Core/SharpOS.Kernel.Core.csproj	(revision 1230)
+++ Kernel/Core/SharpOS.Kernel.Core.csproj	(working copy)
@@ -1,8 +1,8 @@
-<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
+﻿<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
   <PropertyGroup>
     <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
     <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
-    <ProductVersion>8.0.50727</ProductVersion>
+    <ProductVersion>9.0.21022</ProductVersion>
     <SchemaVersion>2.0</SchemaVersion>
     <ProjectGuid>{1BDA3EE0-BC5F-4C4A-986C-31730A8EFADC}</ProjectGuid>
     <OutputType>Library</OutputType>
@@ -78,6 +78,7 @@
     </ProjectReference>
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="ADC\X86\APM.cs" />
     <Compile Include="ADC\X86\Architecture.cs" />
     <Compile Include="ADC\X86\AssemblyAttributes.cs" />
     <Compile Include="ADC\X86\Barrier.cs" />
@@ -267,6 +268,7 @@
     <Compile Include="Shell\Commands\BuiltIn\MemDump.cs" />
     <Compile Include="Shell\Commands\BuiltIn\MemView.cs" />
     <Compile Include="Shell\Commands\BuiltIn\Panic.cs" />
+    <Compile Include="Shell\Commands\BuiltIn\PowerOff.cs" />
     <Compile Include="Shell\Commands\BuiltIn\Reboot.cs" />
     <Compile Include="Shell\Commands\BuiltIn\Show.cs" />
     <Compile Include="Shell\Commands\BuiltIn\Snake.cs" />
Index: Kernel/Core/Shell/Commands/BuiltIn/PowerOff.cs
===================================================================
--- Kernel/Core/Shell/Commands/BuiltIn/PowerOff.cs	(revision 0)
+++ Kernel/Core/Shell/Commands/BuiltIn/PowerOff.cs	(revision 0)
@@ -0,0 +1,55 @@
+// 
+// (C) 2007 The SharpOS Project Team (http://www.sharpos.org)
+//
+// Authors:
+//	airstep <back.neomind@gmail.com>
+//
+// Licensed under the terms of the GNU GPL v3,
+//  with Classpath Linking Exception for Libraries
+//
+
+using System;
+using System.Collections.Generic;
+using System.Text;
+using SharpOS.AOT.Attributes;
+using SharpOS.Kernel.Foundation;
+using SharpOS.Kernel.ADC;
+
+namespace SharpOS.Kernel.Shell.Commands.BuiltIn
+{
+    public unsafe static class PowerOff
+    {
+        public const string name = "poweroff";
+        public const string shortDescription = "Turn off the machine";
+        public const string lblExecute = "COMMANDS.poweroff.Execute";
+        public const string lblGetHelp = "COMMANDS.poweroff.GetHelp";
+
+        [Label(lblExecute)]
+        public static void Execute(CommandExecutionContext* context)
+        {
+            EntryModule.PowerOff();
+        }
+
+        [Label(lblGetHelp)]
+        public static void GetHelp(CommandExecutionContext* context)
+        {
+            TextMode.WriteLine("Syntax: ");
+            TextMode.WriteLine("     poweroff");
+            TextMode.WriteLine("");
+            TextMode.WriteLine("Turn off the machine.");
+        }
+
+        public static CommandTableEntry* CREATE()
+        {
+            CommandTableEntry* entry = (CommandTableEntry*)SharpOS.Kernel.ADC.MemoryManager.Allocate((uint)sizeof(CommandTableEntry));
+
+            entry->name = (CString8*)SharpOS.Kernel.Stubs.CString(name);
+            entry->shortDescription = (CString8*)SharpOS.Kernel.Stubs.CString(shortDescription);
+            entry->func_Execute = (void*)SharpOS.Kernel.Stubs.GetLabelAddress(lblExecute);
+            entry->func_GetHelp = (void*)SharpOS.Kernel.Stubs.GetLabelAddress(lblGetHelp);
+            entry->nextEntry = null;
+
+            return entry;
+        }
+    }
+}
Index: Kernel/Core/Shell/Commands/CommandTableHeader.cs
===================================================================
--- Kernel/Core/Shell/Commands/CommandTableHeader.cs	(revision 1230)
+++ Kernel/Core/Shell/Commands/CommandTableHeader.cs	(working copy)
@@ -275,6 +275,7 @@
 			header->AddEntry (BuiltIn.MemView.CREATE ());
 			header->AddEntry (BuiltIn.Panic.CREATE ());
 			header->AddEntry (BuiltIn.Reboot.CREATE ());
+            header->AddEntry(BuiltIn.PowerOff.CREATE());
 			header->AddEntry (BuiltIn.Show.CREATE ());
 			header->AddEntry (BuiltIn.Snake.CREATE ());
 			header->AddEntry (BuiltIn.Stage.CREATE ());
Index: Tools/DiagnosticTool/DiagnosticTool.csproj
===================================================================
--- Tools/DiagnosticTool/DiagnosticTool.csproj	(revision 1230)
+++ Tools/DiagnosticTool/DiagnosticTool.csproj	(working copy)
@@ -48,12 +48,18 @@
     <Compile Include="AssemblyInfo.cs" />
     <Compile Include="Client.cs" />
     <Compile Include="INamedPipe.cs" />
-    <Compile Include="MainWindow.cs" />
+    <Compile Include="MainWindow.cs">
+      <SubType>Form</SubType>
+    </Compile>
     <Compile Include="MainWindow.Designer.cs" />
-    <Compile Include="MemoryView.cs" />
+    <Compile Include="MemoryView.cs">
+      <SubType>Form</SubType>
+    </Compile>
     <Compile Include="MemoryView.Designer.cs" />
     <Compile Include="Program.cs" />
-    <Compile Include="UnitTestsView.cs" />
+    <Compile Include="UnitTestsView.cs">
+      <SubType>Form</SubType>
+    </Compile>
     <Compile Include="UnitTestsView.Designer.cs" />
     <Compile Include="UnixNamedPipe.cs" />
     <Compile Include="WindowsNamedPipe.cs" />
Index: Tools/DiagnosticTool/MainWindow.cs
===================================================================
--- Tools/DiagnosticTool/MainWindow.cs	(revision 1230)
+++ Tools/DiagnosticTool/MainWindow.cs	(working copy)
@@ -110,7 +110,7 @@
 			this.Invoke (this.dMessage, "OK. Pipe opened.");
 			this.Invoke (this.dUpdateGUI);
 
-			while (true)
+            while (!vm.HasExited)
 			{
 				string status = string.Format ("R: {0} / W: {1}", Client.BytesRead, Client.BytesWritten);
 				this.Invoke (this.dSetStatus, status);
@@ -121,6 +121,9 @@
 
 				Thread.Sleep (100);
 			}
+
+            this.Invoke (this.dMessage, "VM is turned off...");
+            this.BeginInvoke ((MethodInvoker)delegate { this.Close (); });
 		}
 
 		private Thread waitForVMThread;
