Before the release of official PwnageTool 4.0, only way to jailbreak iPhone 3GS iOS 4.0 is by using PwnageTool bundles created by MSFT.guy. These bundles actually act as a workaround to allow PwnageTool 3.1.5. Now iPhone Dev Team has officially released PwnageTool 4.0, it is recommended to use this official latest release of PwnageTool to jailbreak your iPhone 3GS.
Before you may start jailbreaking iPhone 3GS with PwnageTool 4.0, please read the official note from iPhone Dev Team below:
If your have iPhone 3GS with OLD BOOTROM, and you haven't used Spirit tool to jailbreak your iPhone earlier, then proceed to our step-by-step guide to jailbreak iPhone 3GS iOS 4.0 with PwnageTool 4.0 here.
Once jailbreaked, you can use Ultrasn0w 0.93 to unlock your iPhone 3GS. You can find detailed step-by-step instructions here.