From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from NAM01-BN3-obe.outbound.protection.outlook.com (mail-bn3nam01on0049.outbound.protection.outlook.com [104.47.33.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 1019221C943FE for ; Thu, 22 Jun 2017 13:37:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector1-amd-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=SRzynG4kNaV2OGN7bdhk/xzq3xn0cxr2Jel9agVBE1Y=; b=wKSnn/baJd1zFDYqVrtEylgjGUrf4ue25+GAgrMP4xbr6osczKzCtN6Kl+V4Lx6dCNWXahGthPb/SoKjIXWt7rFQ081TnWaMRBR4ImODxYMNDlIpz0a/T7umVIFOjo77DGUF5aG6MmBELwJGJ0RAblLF/ZOZ3zTsOWlqWH4r1Fo= Authentication-Results: lists.01.org; dkim=none (message not signed) header.d=none;lists.01.org; dmarc=none action=none header.from=amd.com; Received: from brijesh-build-machine.amd.com (165.204.77.1) by BY2PR12MB0145.namprd12.prod.outlook.com (10.162.82.18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1199.15; Thu, 22 Jun 2017 20:38:26 +0000 From: Brijesh Singh To: edk2-devel@lists.01.org Cc: Brijesh Singh , Jordan Justen , Laszlo Ersek Date: Thu, 22 Jun 2017 16:37:35 -0400 Message-Id: <1498163868-30191-5-git-send-email-brijesh.singh@amd.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1498163868-30191-1-git-send-email-brijesh.singh@amd.com> References: <1498163868-30191-1-git-send-email-brijesh.singh@amd.com> MIME-Version: 1.0 X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: CO1PR15CA0051.namprd15.prod.outlook.com (10.175.176.19) To BY2PR12MB0145.namprd12.prod.outlook.com (10.162.82.18) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 14b5cc29-1b1f-494a-c976-08d4b9aea26d X-MS-Office365-Filtering-HT: Tenant X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001)(48565401081)(201703131423075)(201703031133081); SRVR:BY2PR12MB0145; X-Microsoft-Exchange-Diagnostics: 1; BY2PR12MB0145; 3:NQZHhfhyUCORlgHu/ysN4R0f3Wg+FDHKSkmsFTA0hpjxDuA5LMCrYvboxNvTe7fmbgMeoCRThvO/jzq8e7gafpPAW/H3YpvPTDoWpXreiq6b22qvW/8Q4Vk0m9AC3dgT50Q+ToAA4z/Bc85stLnUT+9cyMK+HYIT6zaWBXO6cp75QX45RdMo8cMTSc8GMm7fgcpvgJxJeJ555FoEXV9LQR8lrq24jhgryzTyJsIovWtoisoOiIVKgLpIoXLiYO7oX2IsUEF196DokSFZL9XHflVbJiCZ09RcFs5uzPixy2echA0jvzeiLYv+ic+68BAmtszrGqMVQAdSOF0tlTGGlU5DhrldVwOtaxzTB3KcM+k=; 25:e4tbG7gvAl96XzqOtweSFQ2dZpdFzuy7hA1Y/Xk7wZi8OC6CJ17c8xRfb85gTzaTvlAEC9lYs7TjrPwJJRVAs5DSOKrg3jJ9BCuOM78w2H7fcTYOiZ09Z6XRnaWdEztyiIacGGWJoJA4DOkJEhwo6wz01YA1ytuCY8c0r3n3NI0cmObnbDNr1FGgtBXJW4QJ/4Hgb25hWHfgOyjgIMikw+liQk/xHJMteHAJZb3w5KodWPgBvwmccSgnAfj/MDK0Q4+X7u3QI+AvoJftA5dmOnAJbV6wGRezMbDe2GujcVdEntcZ2+wZtcNq+so3Os5s1EA1P0B+Vn6DssAmvVJD3ZejR8tqsgdcLSqH+TFxDFUj91zoPj0ASL5R6M55k5vCZTI7sJ08CyQm+nny1n88NrJdDeMl3U4mLASQ5NTRf0EMFdztU+S9iaRc/pHDnXoSUaJvgs0DcbtXMP9Rqhq2YrGBSxzSDDDhW3Gi+13fiiw= X-MS-TrafficTypeDiagnostic: BY2PR12MB0145: X-Microsoft-Exchange-Diagnostics: 1; BY2PR12MB0145; 31:cYN51h98q9FOghT756FHv3fZOT6XfTMcEmGiU4O6H/WRzcWwWnnskw/0+MSuI7YZ5ao4tLIEvPWgU782MVrCyTV1eEwRu4EDMUVueOfaUdMBeaw2vu37hePayx18S6VtydnvMAiRoJo+6MjKp+ieGsohzW2s+mjUtgdvIkHQpwO9u2wk6QwN/bOFBaVB1H42G+Uq+47aILUvElbKM5q1H6U4ZWA0nJ2imGdXB2mstwg=; 20:JOiTBrETBu2t07jHhZddz+dKLDKxizrzIUWTswENanm6E1t0ji52yshbMvCdb+ORimg0saikzULKHbiuJAn5WNHDgLAUebjjRV5qNAqOI0PQRWs87J52+MbFzquTLyAmnUWnrfApoLJD9k7dQa78IGh/PDQPp63pi/+IML5YA90+oa6EgBPxuTBx4HjnrUE9cU+SZfmE7ziYHmQH+gGpmDVFGvBrtOuDiwDMoczZLSGGSXQLxTetCdDxKzaug6MfYVCDNxO2aqz0myNXfaZcAIzeNGiJ/5K2iiQhtG9x688V9lwIWmFoDu3XgQuphXDhnlQQOYNLzL8EPH+fW6AFcltKFqsvn66CGlPNnwO+/+iuxuuMdCzvVyz7YZvzNszqFsKD5XzBV7D1OUtkYPhyNl/aeILDJ6rdlq92GX261zlI2J5khHcV1V9wWPNKfbC3khA7cylTtubdF6hdDQS/0sO7Psq+QgdYQRYv2yfvAyO6fXIjqj4fE4vTQd25X5Dv X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(767451399110)(228905959029699); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(100000700101)(100105000095)(100000701101)(100105300095)(100000702101)(100105100095)(6040450)(601004)(2401047)(8121501046)(5005006)(10201501046)(100000703101)(100105400095)(93006095)(93001095)(3002001)(6055026)(6041248)(20161123564025)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123562025)(20161123555025)(20161123558100)(20161123560025)(6072148)(100000704101)(100105200095)(100000705101)(100105500095); SRVR:BY2PR12MB0145; BCL:0; PCL:0; RULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(100000804101)(100110200095)(100000805101)(100110500095); SRVR:BY2PR12MB0145; X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; BY2PR12MB0145; 4:WzBgs5cPispW0NLOVg8ZKFHRbaD/53NjG4B0a0JlKD?= =?us-ascii?Q?dipOArK9FaGebQ3HgLKN/6Xo5DCHYuSajTML+HVjr76lbSgsf6lZ3XYOmxxt?= =?us-ascii?Q?GvPtM9oJngRjpFjhV2t73ADvOJlBMHJEwECQsnG440MWqdT0Mb8AiZHYhOZe?= =?us-ascii?Q?C41Q3Vl+QofoNNHUqRU+j1k8rdwabb4LhlKT8+VbSvAg1Gm0heG/RqyGoIRA?= =?us-ascii?Q?SjeXKVKRJ1ybzE3P5zrmi+duE3zqOBLFymFOnILKsJ0cgVzB+SZVfIyqA6hK?= =?us-ascii?Q?WSTewd0DyDcabxQ8y3UYyYVc+RvRFwe1xUDbVTu9xbM2KXr2ptf4cSG4dSug?= =?us-ascii?Q?B3mLImKtEtTLprhV994W/fpnqxUa0LO/w/nygOtGPuOYbi/baETE0th3s6La?= =?us-ascii?Q?vJpYdA6JWEbCNv6G+AMxIT5b0jq9CcvF5+AoUyAZDa6pTIwjyX+onRZ/51+E?= =?us-ascii?Q?R8UhIIdAleIxs72Ffm76/F4FTOW1QxdcNdZ6nIVmGVD9Cjga3oohgojsrDDP?= =?us-ascii?Q?zPQPx7Zwdg2HEozfOKS4OycNArcMO8cuCLlTHRafJrYgmOs7kHIV5/yJXY0U?= =?us-ascii?Q?iLnQw0UR/TPBOjZpuEuHKCyl/EQX8ffpS6VSKm9CgX709KsS0p6BKzbvQOKP?= =?us-ascii?Q?swUVvZpGqPm6q3Fdsjv0ApiKqprODV/ubKorqExgaYhej7cJDJmsBDPcyzIA?= =?us-ascii?Q?2kQ5+SxEWaZpjUUW/JJ68cq5se48S5vdN5P4PY0ltW7v1UJkq22NMiI78IcO?= =?us-ascii?Q?AuQYPWICXy/lvHP4cdr9zqtQt5VLtmmWca/TE6cqFDj7RwClOLOzuyt5FOB6?= =?us-ascii?Q?kRgOvzLf8cPqWiLyt8IUBu39e6pn4vYljHOgQxp4sBQ7SdPox5i1zxXkpv06?= =?us-ascii?Q?EYu/iJqEDDKKRbgTry8Diz065d4koIy9nZOaCFr7++sJfOEzXpaT2L4roNdg?= =?us-ascii?Q?x6dHCjtE0qfvqG8jIFeBeMJtYYBfd18U/3Mded2ryuxXEs5JgtWZvaw8qL1C?= =?us-ascii?Q?sO3CIvZ/GRAiLzcdt7p87VuapoTlpn3kU3XzDHiA3b0eXIYGbhLqvrDYdF2T?= =?us-ascii?Q?IFbFXFBXn1v0YF2O36ZunK61piSehIHTVEfTdiqnTRuyI7Nz2EC/Jxqp8oji?= =?us-ascii?Q?XwclZPX4FImckARujgqbtdOp7HVGDfXMb9cwvF2IWgA7OSQ9cwmhy0sX7xqE?= =?us-ascii?Q?ennz504x/P0oG49isDRz93E5JQvExLgnxEC/gBeexRpvysLKJ06OP1cbYy4g?= =?us-ascii?Q?YbSqUt/5WwgCE1LhI=3D?= X-Forefront-PRVS: 03468CBA43 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(4630300001)(6009001)(39450400003)(39840400002)(39850400002)(39860400002)(39410400002)(39400400002)(25786009)(189998001)(50226002)(76176999)(50986999)(2351001)(2361001)(7736002)(33646002)(2950100002)(6916009)(6666003)(6486002)(4326008)(53936002)(2906002)(110136004)(15188155005)(5003940100001)(16799955002)(8676002)(81166006)(38730400002)(478600001)(53376002)(966005)(36756003)(6306002)(53946003)(305945005)(54906002)(47776003)(3846002)(66066001)(50466002)(48376002)(6116002)(86362001)(53416004)(5660300001)(575784001)(42186005)(579004)(19627235001); DIR:OUT; SFP:1101; SCL:1; SRVR:BY2PR12MB0145; H:brijesh-build-machine.amd.com; FPR:; SPF:None; MLV:sfv; LANG:en; X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; BY2PR12MB0145; 23:kgjDRwA+Sxyp/AUSUVr8QTly98zYnlYOSHRPJAbB+?= =?us-ascii?Q?mSMY0s7fKAniInB7Y/cTqxBqLEXnIWVajxP8sXkKOSYgByB7SCKIBAhru2Iq?= =?us-ascii?Q?/uEMuSGUddCUUM8B5P/vgHKH5dbTqdjJoWTEUBqT2L0NotJRXVTP0cIRYPAD?= =?us-ascii?Q?qJq5KDhfJUwzj/G2jKqR1ok+JkZLlocwMlwh/1SaZZ4BSZEkOQsh4Wx2Q6lq?= =?us-ascii?Q?pN+vRPKB0k3e2pTDemyul/NeloHEGbcGNjUPFoq7sH49PuETPm+XW/cMtBJo?= =?us-ascii?Q?VkYm0T5QDoDyZKQF46seJ2OPPAvUwQG/WGmSSmbbMRcP9d1TxXj0RVBvwYtb?= =?us-ascii?Q?hfrI+dvCfeon1s1hLNwZlBj7wAhv/iv197Z5UQX2dXnSyS+FUPv3VzTbPc5u?= =?us-ascii?Q?nAZzKqIDBDhNPDr1L3aysjxmRQ5ILKcfBAUE+3Z8voTooYfWr0pi2Rb3qwDR?= =?us-ascii?Q?Mv7Yt0z1yA8LKkfiiJY5uOCfU/xpbLEMKNSeaS7V9UXXeBIN384QlMA6H58I?= =?us-ascii?Q?iizgx6mtx2msFg83a18UHvlwgC+gwKD7uVqNGCnI+zAP3kbzLvHgfyZ8ypcM?= =?us-ascii?Q?Cc4/JGMXy3+JOD48KsFHY+Jc3vUX+e4ez6g9iGgXEbxHLCQrrVzaPlVagN2N?= =?us-ascii?Q?VRuRQDUSbnLgS1KkpKYByq9E3ylOIsz1TSlFnk2ssRJb+1KGOQ1U5kQnlK+S?= =?us-ascii?Q?CX/aLXqcLXqJJLol3VanJTd7BfK/ahPv2bSTZ+8HoqL0UiJRPB2RuLZ30XvS?= =?us-ascii?Q?gMZp+YdtwYgCe4gMeD+RbxaOVhMPHMZMVmsMJTRU5lfDmX1IdNn7Yce5wAGM?= =?us-ascii?Q?egIeFrD0/j4NpQMUayutTYTbJZu25F++pIaToRKxTlLZI2wfO1oSfXcsMOjM?= =?us-ascii?Q?GL4eyA2CDPsWtAqenDNsEp8s+SYfJn4h6HzHCP9sQ3kHEl79W9COhvlzNZ5Y?= =?us-ascii?Q?YrZ1aAWCgjiykBcBCQxyBjTIzn5GenLS2G7ihLLL/M4LOkxARwDnTzeFLg8t?= =?us-ascii?Q?am8oBxbQ81MS4ook7tzDaHZqg0eGcJCFZl+YFQ6k4/WsSxdTeBHTkaiBGgkf?= =?us-ascii?Q?TaTLCM9RzGIi806pi/7QIjsUw44idc+JplvQZBelnoJaya4FnHBa+KFKLHWd?= =?us-ascii?Q?DbSyyCQ23CKO72HgfNGuv8bDlLJN/iUPA2Ol+g1KJVxjybhXs0Gb70TWEBRg?= =?us-ascii?Q?+nJzslkIbEkfpHY7ls76tFXNVxqnPUSMgwXaGEx0aUK1lTW7QUKAQghvDdKH?= =?us-ascii?Q?00WOVbjT6hQmuZPidN4yekKXWm3QKBtFjmHX/u7kjLcN62rSv1J6xXbBT9Ro?= =?us-ascii?B?UT09?= X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; BY2PR12MB0145; 6:kHrOXHCVVAYWMOCoE3fABURfCVNE4KdE7pPmjAP/iE?= =?us-ascii?Q?mQlwAjLEOrWsGPIkfKVyZ/sLRtmlEhjZch4w81M+FWXqCzONix6d92xEVXFL?= =?us-ascii?Q?sxQCtdMcBd1lipiN0K8+STyIf7P3mU7YDy0lbMj0UPpn07ia+RNnlkP76DTY?= =?us-ascii?Q?7iRUohVhDwqOYL4RNdsunHIzuSUZ3JmgTWbmoBDNZ68Dy+F4frmS3PxPEvdi?= =?us-ascii?Q?EGc/+PVM/0IaQ4upLqcGDcidy2OfJFl7ZywUcrZFQH+WNPqH72GR1mqhBVVg?= =?us-ascii?Q?sEOMQjFSrIHlJC8ZZAUlCJlHkNdjwl2WFiw4t1N8t9p5pkaOvdUmr9opAkGT?= =?us-ascii?Q?lvm2+IG36vrT9WSi862TNaDw1m4LB7GS2Io4zA385wm0FISwjcqjDNgln1XW?= =?us-ascii?Q?7sBplkkSfslIb8iaUvFHUc52NSgL868St1Fv5yWO1XoOsYBKno7oQSQ/qjQr?= =?us-ascii?Q?CRgb9DBWRQ50RzesJfQldGgK9b+H2Px4s//BT7qbpofr1+cxUmW3k+ka2kCd?= =?us-ascii?Q?VACFPDMT84TNyFXqt3Tivbugu94vqkklfzTyR0QmR/RK7IjhxOxzYW4YrRBK?= =?us-ascii?Q?ZWSaRDOQv6omX/GzV3NYi+qkrrnbaokB0VfBaqloA8V7HagDU6ckSaNq2plo?= =?us-ascii?Q?7baj2cQnYcc7ea6Xmr0TFDiQt8/gnAwrRamteJR9+kw5qQtfFGJ18G4bSkr5?= =?us-ascii?Q?8574jh9S7kF4g9bcg4yHczILhx1mJw/pfN4fXvTI+DyWvTWZBGoWNLp9Y/7T?= =?us-ascii?Q?dxmKRGzOkDgLgcoQzMCVfYHYyNKUTmCMjGVFXeVDa5hNA9qbHQwgjkhfTMdR?= =?us-ascii?Q?4kscdFn3QWiwX6uuC7gm+9ffKP7IVM8jXCnR82u5VuLaF/P1s9a4sKi08gAW?= =?us-ascii?Q?B6thE0mlRuzMgR5ZFLNXbybG3udS1m8QGq4vLouEKrWpw1VQGzREwVUWP7wa?= =?us-ascii?Q?sO9xwzd/4hkdiK99f2CIlrxN1i1px2YH1f+E3R3qVeyypm2RRi0A9rAEtxm/?= =?us-ascii?Q?ElsBYKSIulFgy1bfkh1kqm?= X-Microsoft-Exchange-Diagnostics: 1; BY2PR12MB0145; 5:R4c79Sb37l5R0BOMeAzCQQ8uA3uNHf69wP4mnfv9Xe+1YhTq1aQJ9LRJOddqjLM+W8bscxMzhMqfTYECi74YjEVhakynH5HnPPMjdiY5TCEdiwrm6M10a26NWAZELztrr72tMc2MzRRNPXkWE9WdOColNhzVfFku+AimtKxdtbS7qKYefHl3spND2Ikdjvke5wStnNnqvb2/uofHV4Mz1Nar/onC5o/BiihHAzyr9PCDbPMyWh7JEb/rfkNRPXh6dnBmwYqzl3l9/Xui6cU+rllTaEtVSwWGAUR6X/AZ92XNtFv1KhUlsCFwKAn7Vi+/UOUYyWSREiPBzrFGXcjeLlJNDtQG2lTkdP/dEYNWGurbrtNRNViHquh1GIpThKdTbvhMPbStcfXJHgNXFJBpY3U+dKcf39BEs1W4zsILKn0pDUulR2zjxKGWdtUXTFYGl0rqxtZWAJFooN+1rx5wKlKyPqiJsicD2dqM/S0amoOu6bevxURZ/lH6iYreOUSh; 24:3kiNmnv5fQG5Vu4P8gB2XpMm+qkyM6RI8+l4S8LLwLW6Bx9/homUju2kUEEvhhbfZt953CWFRP6CY6Yvt9hqXGf05NJ0mTWbxFS+MGWADms= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; BY2PR12MB0145; 7:/hpOqBTuyuZOQtTY4oZzNfQnVCMFzfYuzK323qWOkITYOvt1e4+Qq5uhj15K+xCuKCVb8TCzLrGLwdm5ZOOwT9jgoVVZUdQqqTmi5ag9iOBgVn9R8hauoJySLHMijFtFhjT0ZW90wxwr+xGZMbFQri18030aW+NxS+PGxya8DTYPgW55Fj9D0UcfAs7zHCBsfpNAwk8PWQzL2rd75rK6bB6lgX2BQ18vwy6sP8UF9UGu2mU9gIyT1BBNfuZJ3jur0Dtc1O0sbfluI8r1492KXsy0vH15Hwl9zAjclWkyoUJ6DCGHX7cVDvr5Jb50E0d/KkgP3RTGnWDQVERa2LmCE6QTCAvIquKrYDbV+bbF2d0nvlicPBxFKW3C81GrHcD5NP31PbQrBd+wekerCMzYEfuKSCvgZI+WdByUGruzYPVcB726vz4fgP6uNgLTfswPXZcrXPFH+ZfFoYmt/SOhyI+JP8jVbnNkvPnKoLmIJReieZxGTP9I2eLZxU7RH8yMyayXPkH9Q7VSg0c96xc5Xwc8jSDAI0IR7a5KmzO1tR/17zROCPdE85aQ6hY3MKeo15RoAdNWsQxeD975ha4lVlCNQG4+ndduopmzpmCyV8cghxylZwiCPbozcSuLf9t5wnolNb1iFUS93uzBXTNrAkT4HC9/MFcZmP3OzhUEAn0K5Yb8rAaFVGgd+wurpmQjdi+VD3JbMZ6p3mTxhL9/RL07kudDRzmhlSpxGSUHsI9p1keGET71XGem7frC0PGAU+SKDW/5ptbgm85kivrpEJgabxFc2LKsyJe4uC7Gr64= X-Microsoft-Exchange-Diagnostics: 1; BY2PR12MB0145; 20:bzA6QQDcRPUWGaounJjwkpYB6dN2wXaYpTdNYdxKHbu+NU5LoNYJGF72YJyoBm2omz9mICqwehyCK1lwEOhgAuiLKM3499cQVNmzg4ugXfwN9dcR+Z69gb0Oqnt/hc/wfX2bsXOXDbqmMfL+9fLl6+rc47c0riJgg4nUEkAfEvmpwCHqsLZ2ERME9p+ON8kUghoSsh7iXl8Z00BxdukpTd+KsyGtDcaCLOnYdtqk5xO2XnKVIitlwqU2CdW+bQ87 X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Jun 2017 20:38:26.1322 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY2PR12MB0145 Subject: [PATCH v7 04/17] OvmfPkg/BaseMemcryptSevLib: Add SEV helper library X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 22 Jun 2017 20:37:03 -0000 Content-Type: text/plain Add Secure Encrypted Virtualization (SEV) helper library. The library provides the routines to: - set or clear memory encryption bit for a given memory region. - query whether SEV is enabled. Cc: Jordan Justen Cc: Laszlo Ersek Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Brijesh Singh Reviewed-by: Laszlo Ersek --- OvmfPkg/OvmfPkgIa32.dsc | 1 + OvmfPkg/OvmfPkgIa32X64.dsc | 1 + OvmfPkg/OvmfPkgX64.dsc | 1 + OvmfPkg/Library/BaseMemEncryptSevLib/BaseMemEncryptSevLib.inf | 50 +++ OvmfPkg/Include/Library/MemEncryptSevLib.h | 81 ++++ OvmfPkg/Library/BaseMemEncryptSevLib/X64/VirtualMemory.h | 184 ++++++++ OvmfPkg/Library/BaseMemEncryptSevLib/Ia32/MemEncryptSevLib.c | 84 ++++ OvmfPkg/Library/BaseMemEncryptSevLib/MemEncryptSevLibInternal.c | 90 ++++ OvmfPkg/Library/BaseMemEncryptSevLib/X64/MemEncryptSevLib.c | 84 ++++ OvmfPkg/Library/BaseMemEncryptSevLib/X64/VirtualMemory.c | 439 ++++++++++++++++++++ 10 files changed, 1015 insertions(+) diff --git a/OvmfPkg/OvmfPkgIa32.dsc b/OvmfPkg/OvmfPkgIa32.dsc index b5c874b65afc..98691c0a6d99 100644 --- a/OvmfPkg/OvmfPkgIa32.dsc +++ b/OvmfPkg/OvmfPkgIa32.dsc @@ -145,6 +145,7 @@ [LibraryClasses] QemuFwCfgLib|OvmfPkg/Library/QemuFwCfgLib/QemuFwCfgLib.inf VirtioLib|OvmfPkg/Library/VirtioLib/VirtioLib.inf LoadLinuxLib|OvmfPkg/Library/LoadLinuxLib/LoadLinuxLib.inf + MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/BaseMemEncryptSevLib.inf !if $(SMM_REQUIRE) == FALSE LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf !endif diff --git a/OvmfPkg/OvmfPkgIa32X64.dsc b/OvmfPkg/OvmfPkgIa32X64.dsc index 1d071cac2bef..3b51513a4d95 100644 --- a/OvmfPkg/OvmfPkgIa32X64.dsc +++ b/OvmfPkg/OvmfPkgIa32X64.dsc @@ -150,6 +150,7 @@ [LibraryClasses] QemuFwCfgLib|OvmfPkg/Library/QemuFwCfgLib/QemuFwCfgLib.inf VirtioLib|OvmfPkg/Library/VirtioLib/VirtioLib.inf LoadLinuxLib|OvmfPkg/Library/LoadLinuxLib/LoadLinuxLib.inf + MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/BaseMemEncryptSevLib.inf !if $(SMM_REQUIRE) == FALSE LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf !endif diff --git a/OvmfPkg/OvmfPkgX64.dsc b/OvmfPkg/OvmfPkgX64.dsc index 2bea75437788..ebf07cd585dd 100644 --- a/OvmfPkg/OvmfPkgX64.dsc +++ b/OvmfPkg/OvmfPkgX64.dsc @@ -150,6 +150,7 @@ [LibraryClasses] QemuFwCfgLib|OvmfPkg/Library/QemuFwCfgLib/QemuFwCfgLib.inf VirtioLib|OvmfPkg/Library/VirtioLib/VirtioLib.inf LoadLinuxLib|OvmfPkg/Library/LoadLinuxLib/LoadLinuxLib.inf + MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/BaseMemEncryptSevLib.inf !if $(SMM_REQUIRE) == FALSE LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf !endif diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/BaseMemEncryptSevLib.inf b/OvmfPkg/Library/BaseMemEncryptSevLib/BaseMemEncryptSevLib.inf new file mode 100644 index 000000000000..3cfd80a28c1d --- /dev/null +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/BaseMemEncryptSevLib.inf @@ -0,0 +1,50 @@ +## @file +# Library provides the helper functions for SEV guest +# +# Copyright (c) 2017 Advanced Micro Devices. All rights reserved.
+# +# This program and the accompanying materials +# are licensed and made available under the terms and conditions of the BSD +# License which accompanies this distribution. The full text of the license +# may be found at http://opensource.org/licenses/bsd-license.php +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. +# +# +## + +[Defines] + INF_VERSION = 1.25 + BASE_NAME = MemEncryptSevLib + FILE_GUID = c1594631-3888-4be4-949f-9c630dbc842b + MODULE_TYPE = BASE + VERSION_STRING = 1.0 + LIBRARY_CLASS = MemEncryptSevLib|PEIM DXE_DRIVER DXE_RUNTIME_DRIVER DXE_SMM_DRIVER UEFI_DRIVER + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 +# + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + OvmfPkg/OvmfPkg.dec + UefiCpuPkg/UefiCpuPkg.dec + +[Sources.X64] + MemEncryptSevLibInternal.c + X64/MemEncryptSevLib.c + X64/VirtualMemory.c + +[Sources.IA32] + MemEncryptSevLibInternal.c + Ia32/MemEncryptSevLib.c + +[LibraryClasses] + BaseLib + CpuLib + CacheMaintenanceLib + DebugLib + MemoryAllocationLib diff --git a/OvmfPkg/Include/Library/MemEncryptSevLib.h b/OvmfPkg/Include/Library/MemEncryptSevLib.h new file mode 100644 index 000000000000..b6753762423e --- /dev/null +++ b/OvmfPkg/Include/Library/MemEncryptSevLib.h @@ -0,0 +1,81 @@ +/** @file + + Define Secure Encrypted Virtualization (SEV) base library helper function + + Copyright (c) 2017, AMD Incorporated. All rights reserved.
+ + This program and the accompanying materials are licensed and made available + under the terms and conditions of the BSD License which accompanies this + distribution. The full text of the license may be found at + http://opensource.org/licenses/bsd-license.php + + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + +**/ + +#ifndef _MEM_ENCRYPT_SEV_LIB_H_ +#define _MEM_ENCRYPT_SEV_LIB_H_ + +#include + +/** + Returns a boolean to indicate whether SEV is enabled + + @retval TRUE SEV is active + @retval FALSE SEV is not enabled + **/ +BOOLEAN +EFIAPI +MemEncryptSevIsEnabled ( + VOID + ); + +/** + This function clears memory encryption bit for the memory region specified + by BaseAddress and Number of pages from the current page table context. + + @param[in] BaseAddress The physical address that is the start address + of a memory region. + @param[in] NumberOfPages The number of pages from start memory region. + @param[in] Flush Flush the caches before clearing the bit + (mostly TRUE except MMIO addresses) + + @retval RETURN_SUCCESS The attributes were cleared for the memory region. + @retval RETURN_INVALID_PARAMETER Number of pages is zero. + @retval RETURN_UNSUPPORTED Clearing memory encryption attribute is not + supported + **/ +RETURN_STATUS +EFIAPI +MemEncryptSevClearPageEncMask ( + IN PHYSICAL_ADDRESS Cr3BaseAddress, + IN PHYSICAL_ADDRESS BaseAddress, + IN UINTN NumberOfPages, + IN BOOLEAN CacheFlush + ); + +/** + This function sets memory encryption bit for the memory region specified by + BaseAddress and Number of pages from the current page table context. + + @param[in] BaseAddress The physical address that is the start address + of a memory region. + @param[in] NumberOfPages The number of pages from start memory region. + @param[in] Flush Flush the caches before clearing the bit + (mostly TRUE except MMIO addresses) + + @retval RETURN_SUCCESS The attributes were set for the memory region. + @retval RETURN_INVALID_PARAMETER Number of pages is zero. + @retval RETURN_UNSUPPORTED Clearing memory encryption attribute is not + supported + **/ +RETURN_STATUS +EFIAPI +MemEncryptSevSetPageEncMask ( + IN PHYSICAL_ADDRESS Cr3BaseAddress, + IN PHYSICAL_ADDRESS BaseAddress, + IN UINTN NumberOfPages, + IN BOOLEAN CacheFlush + ); +#endif // _MEM_ENCRYPT_SEV_LIB_H_ diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/VirtualMemory.h b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/VirtualMemory.h new file mode 100644 index 000000000000..70cd2187a326 --- /dev/null +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/VirtualMemory.h @@ -0,0 +1,184 @@ +/** @file + + Virtual Memory Management Services to set or clear the memory encryption bit + +Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.
+Copyright (c) 2017, AMD Incorporated. All rights reserved.
+ +This program and the accompanying materials +are licensed and made available under the terms and conditions of the BSD License +which accompanies this distribution. The full text of the license may be found at +http://opensource.org/licenses/bsd-license.php + +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + +Code is derived from MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.h + +**/ + +#ifndef __VIRTUAL_MEMORY__ +#define __VIRTUAL_MEMORY__ + +#include +#include +#include +#include +#include + +#include +#define SYS_CODE64_SEL 0x38 + +#pragma pack(1) + +// +// Page-Map Level-4 Offset (PML4) and +// Page-Directory-Pointer Offset (PDPE) entries 4K & 2MB +// + +typedef union { + struct { + UINT64 Present:1; // 0 = Not present in memory, 1 = Present in memory + UINT64 ReadWrite:1; // 0 = Read-Only, 1= Read/Write + UINT64 UserSupervisor:1; // 0 = Supervisor, 1=User + UINT64 WriteThrough:1; // 0 = Write-Back caching, 1=Write-Through caching + UINT64 CacheDisabled:1; // 0 = Cached, 1=Non-Cached + UINT64 Accessed:1; // 0 = Not accessed, 1 = Accessed (set by CPU) + UINT64 Reserved:1; // Reserved + UINT64 MustBeZero:2; // Must Be Zero + UINT64 Available:3; // Available for use by system software + UINT64 PageTableBaseAddress:40; // Page Table Base Address + UINT64 AvabilableHigh:11; // Available for use by system software + UINT64 Nx:1; // No Execute bit + } Bits; + UINT64 Uint64; +} PAGE_MAP_AND_DIRECTORY_POINTER; + +// +// Page Table Entry 4KB +// +typedef union { + struct { + UINT64 Present:1; // 0 = Not present in memory, 1 = Present in memory + UINT64 ReadWrite:1; // 0 = Read-Only, 1= Read/Write + UINT64 UserSupervisor:1; // 0 = Supervisor, 1=User + UINT64 WriteThrough:1; // 0 = Write-Back caching, 1=Write-Through caching + UINT64 CacheDisabled:1; // 0 = Cached, 1=Non-Cached + UINT64 Accessed:1; // 0 = Not accessed, 1 = Accessed (set by CPU) + UINT64 Dirty:1; // 0 = Not Dirty, 1 = written by processor on access to page + UINT64 PAT:1; // + UINT64 Global:1; // 0 = Not global page, 1 = global page TLB not cleared on CR3 write + UINT64 Available:3; // Available for use by system software + UINT64 PageTableBaseAddress:40; // Page Table Base Address + UINT64 AvabilableHigh:11; // Available for use by system software + UINT64 Nx:1; // 0 = Execute Code, 1 = No Code Execution + } Bits; + UINT64 Uint64; +} PAGE_TABLE_4K_ENTRY; + +// +// Page Table Entry 2MB +// +typedef union { + struct { + UINT64 Present:1; // 0 = Not present in memory, 1 = Present in memory + UINT64 ReadWrite:1; // 0 = Read-Only, 1= Read/Write + UINT64 UserSupervisor:1; // 0 = Supervisor, 1=User + UINT64 WriteThrough:1; // 0 = Write-Back caching, 1=Write-Through caching + UINT64 CacheDisabled:1; // 0 = Cached, 1=Non-Cached + UINT64 Accessed:1; // 0 = Not accessed, 1 = Accessed (set by CPU) + UINT64 Dirty:1; // 0 = Not Dirty, 1 = written by processor on access to page + UINT64 MustBe1:1; // Must be 1 + UINT64 Global:1; // 0 = Not global page, 1 = global page TLB not cleared on CR3 write + UINT64 Available:3; // Available for use by system software + UINT64 PAT:1; // + UINT64 MustBeZero:8; // Must be zero; + UINT64 PageTableBaseAddress:31; // Page Table Base Address + UINT64 AvabilableHigh:11; // Available for use by system software + UINT64 Nx:1; // 0 = Execute Code, 1 = No Code Execution + } Bits; + UINT64 Uint64; +} PAGE_TABLE_ENTRY; + +// +// Page Table Entry 1GB +// +typedef union { + struct { + UINT64 Present:1; // 0 = Not present in memory, 1 = Present in memory + UINT64 ReadWrite:1; // 0 = Read-Only, 1= Read/Write + UINT64 UserSupervisor:1; // 0 = Supervisor, 1=User + UINT64 WriteThrough:1; // 0 = Write-Back caching, 1=Write-Through caching + UINT64 CacheDisabled:1; // 0 = Cached, 1=Non-Cached + UINT64 Accessed:1; // 0 = Not accessed, 1 = Accessed (set by CPU) + UINT64 Dirty:1; // 0 = Not Dirty, 1 = written by processor on access to page + UINT64 MustBe1:1; // Must be 1 + UINT64 Global:1; // 0 = Not global page, 1 = global page TLB not cleared on CR3 write + UINT64 Available:3; // Available for use by system software + UINT64 PAT:1; // + UINT64 MustBeZero:17; // Must be zero; + UINT64 PageTableBaseAddress:22; // Page Table Base Address + UINT64 AvabilableHigh:11; // Available for use by system software + UINT64 Nx:1; // 0 = Execute Code, 1 = No Code Execution + } Bits; + UINT64 Uint64; +} PAGE_TABLE_1G_ENTRY; + +#pragma pack() + +#define IA32_PG_P BIT0 +#define IA32_PG_RW BIT1 + +#define PAGETABLE_ENTRY_MASK ((1UL << 9) - 1) +#define PML4_OFFSET(x) ( (x >> 39) & PAGETABLE_ENTRY_MASK) +#define PDP_OFFSET(x) ( (x >> 30) & PAGETABLE_ENTRY_MASK) +#define PDE_OFFSET(x) ( (x >> 21) & PAGETABLE_ENTRY_MASK) +#define PTE_OFFSET(x) ( (x >> 12) & PAGETABLE_ENTRY_MASK) +#define PAGING_1G_ADDRESS_MASK_64 0x000FFFFFC0000000ull + +/** + This function clears memory encryption bit for the memory region specified by PhysicalAddress + and length from the current page table context. + + @param[in] PhysicalAddress The physical address that is the start address of a memory region. + @param[in] Length The length of memory region + @param[in] Flush Flush the caches before applying the encryption mask + + @retval RETURN_SUCCESS The attributes were cleared for the memory region. + @retval RETURN_INVALID_PARAMETER Number of pages is zero. + @retval RETURN_UNSUPPORTED Setting the memory encyrption attribute is not supported +**/ +RETURN_STATUS +EFIAPI +InternalMemEncryptSevSetMemoryDecrypted ( + IN PHYSICAL_ADDRESS Cr3BaseAddress, + IN PHYSICAL_ADDRESS PhysicalAddress, + IN UINT64 Length, + IN BOOLEAN CacheFlush + ); + +/** + This function sets memory encryption bit for the memory region specified by + PhysicalAddress and length from the current page table context. + + @param[in] PhysicalAddress The physical address that is the start address + of a memory region. + @param[in] Length The length of memory region + @param[in] Flush Flush the caches before applying the + encryption mask + + @retval RETURN_SUCCESS The attributes were cleared for the memory region. + @retval RETURN_INVALID_PARAMETER Number of pages is zero. + @retval RETURN_UNSUPPORTED Setting the memory encyrption attribute is + not supported +**/ +RETURN_STATUS +EFIAPI +InternalMemEncryptSevSetMemoryEncrypted ( + IN PHYSICAL_ADDRESS Cr3BaseAddress, + IN PHYSICAL_ADDRESS PhysicalAddress, + IN UINT64 Length, + IN BOOLEAN CacheFlush + ); + +#endif diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/Ia32/MemEncryptSevLib.c b/OvmfPkg/Library/BaseMemEncryptSevLib/Ia32/MemEncryptSevLib.c new file mode 100644 index 000000000000..a2ea99019917 --- /dev/null +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/Ia32/MemEncryptSevLib.c @@ -0,0 +1,84 @@ +/** @file + + Secure Encrypted Virtualization (SEV) library helper function + + Copyright (c) 2017, AMD Incorporated. All rights reserved.
+ + This program and the accompanying materials + are licensed and made available under the terms and conditions of the BSD + License which accompanies this distribution. The full text of the license may + be found at http://opensource.org/licenses/bsd-license.php + + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + +**/ + +#include +#include +#include +#include +#include +#include + +/** + This function clears memory encryption bit for the memory region specified + by BaseAddress and Number of pages from the current page table context. + + @param[in] Cr3BaseAddress Cr3 Base Address (if zero then use current CR3) + @param[in] BaseAddress The physical address that is the start address + of a memory region. + @param[in] NumberOfPages The number of pages from start memory region. + @param[in] Flush Flush the caches before clearing the bit + (mostly TRUE except MMIO addresses) + + @retval RETURN_SUCCESS The attributes were cleared for the memory region. + @retval RETURN_INVALID_PARAMETER Number of pages is zero. + @retval RETURN_UNSUPPORTED Clearing memory encryption attribute is not + supported + **/ +RETURN_STATUS +EFIAPI +MemEncryptSevClearPageEncMask ( + IN PHYSICAL_ADDRESS Cr3BaseAddress, + IN PHYSICAL_ADDRESS BaseAddress, + IN UINTN NumberOfPages, + IN BOOLEAN Flush + ) +{ + // + // Memory encryption bit is not accessible in 32-bit mode + // + return RETURN_UNSUPPORTED; +} + +/** + This function sets memory encryption bit for the memory region specified by + BaseAddress and Number of pages from the current page table context. + + @param[in] Cr3BaseAddress Cr3 Base Address (if zero then use current CR3) + @param[in] BaseAddress The physical address that is the start address + of a memory region. + @param[in] NumberOfPages The number of pages from start memory region. + @param[in] Flush Flush the caches before clearing the bit + (mostly TRUE except MMIO addresses) + + @retval RETURN_SUCCESS The attributes were set for the memory region. + @retval RETURN_INVALID_PARAMETER Number of pages is zero. + @retval RETURN_UNSUPPORTED Clearing memory encryption attribute is not + supported + **/ +RETURN_STATUS +EFIAPI +MemEncryptSevSetPageEncMask ( + IN PHYSICAL_ADDRESS Cr3BaseAddress, + IN PHYSICAL_ADDRESS BaseAddress, + IN UINTN NumberOfPages, + IN BOOLEAN Flush + ) +{ + // + // Memory encryption bit is not accessible in 32-bit mode + // + return RETURN_UNSUPPORTED; +} diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/MemEncryptSevLibInternal.c b/OvmfPkg/Library/BaseMemEncryptSevLib/MemEncryptSevLibInternal.c new file mode 100644 index 000000000000..002f079c7eb3 --- /dev/null +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/MemEncryptSevLibInternal.c @@ -0,0 +1,90 @@ +/** @file + + Secure Encrypted Virtualization (SEV) library helper function + + Copyright (c) 2017, AMD Incorporated. All rights reserved.
+ + This program and the accompanying materials + are licensed and made available under the terms and conditions of the BSD + License which accompanies this distribution. The full text of the license may + be found at http://opensource.org/licenses/bsd-license.php + + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + +**/ + +#include +#include +#include +#include +#include +#include + +STATIC BOOLEAN mSevStatus = FALSE; +STATIC BOOLEAN mSevStatusChecked = FALSE; + +/** + + Returns a boolean to indicate whether SEV is enabled + + @retval TRUE SEV is enabled + @retval FALSE SEV is not enabled + **/ +STATIC +BOOLEAN +EFIAPI +InternalMemEncryptSevIsEnabled ( + VOID + ) +{ + UINT32 RegEax; + MSR_SEV_STATUS_REGISTER Msr; + CPUID_MEMORY_ENCRYPTION_INFO_EAX Eax; + + // + // Check if memory encryption leaf exist + // + AsmCpuid (CPUID_EXTENDED_FUNCTION, &RegEax, NULL, NULL, NULL); + if (RegEax >= CPUID_MEMORY_ENCRYPTION_INFO) { + // + // CPUID Fn8000_001F[EAX] Bit 1 (Sev supported) + // + AsmCpuid (CPUID_MEMORY_ENCRYPTION_INFO, &Eax.Uint32, NULL, NULL, NULL); + + if (Eax.Bits.SevBit) { + // + // Check MSR_0xC0010131 Bit 0 (Sev Enabled) + // + Msr.Uint32 = AsmReadMsr32 (MSR_SEV_STATUS); + if (Msr.Bits.SevBit) { + return TRUE; + } + } + } + + return FALSE; +} + +/** + + Returns a boolean to indicate whether SEV is enabled + + @retval TRUE SEV is enabled + @retval FALSE SEV is not enabled + **/ +BOOLEAN +EFIAPI +MemEncryptSevIsEnabled ( + VOID + ) +{ + if (mSevStatusChecked) { + return mSevStatus; + } + + mSevStatus = InternalMemEncryptSevIsEnabled(); + mSevStatusChecked = TRUE; + + return mSevStatus; +} diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/MemEncryptSevLib.c b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/MemEncryptSevLib.c new file mode 100644 index 000000000000..9ec76708bd7b --- /dev/null +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/MemEncryptSevLib.c @@ -0,0 +1,84 @@ +/** @file + + Secure Encrypted Virtualization (SEV) library helper function + + Copyright (c) 2017, AMD Incorporated. All rights reserved.
+ + This program and the accompanying materials + are licensed and made available under the terms and conditions of the BSD + License which accompanies this distribution. The full text of the license may + be found at http://opensource.org/licenses/bsd-license.php + + THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, + WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + +**/ + +#include +#include +#include +#include +#include +#include + +#include "VirtualMemory.h" + +/** + + This function clears memory encryption bit for the memory region specified by + BaseAddress and Number of pages from the current page table context. + + @param[in] Cr3BaseAddress Cr3 Base Address (if zero then use current CR3) + @param[in] BaseAddress The physical address that is the start address + of a memory region. + @param[in] NumberOfPages The number of pages from start memory region. + @param[in] Flush Flush the caches before clearing the bit + (mostly TRUE except MMIO addresses) + + @retval RETURN_SUCCESS The attributes were cleared for the memory + region. + @retval RETURN_INVALID_PARAMETER Number of pages is zero. + @retval RETURN_UNSUPPORTED Clearing the memory encryption attribute is + not supported + **/ +RETURN_STATUS +EFIAPI +MemEncryptSevClearPageEncMask ( + IN PHYSICAL_ADDRESS Cr3BaseAddress, + IN PHYSICAL_ADDRESS BaseAddress, + IN UINTN NumPages, + IN BOOLEAN Flush + ) +{ + return InternalMemEncryptSevSetMemoryDecrypted (Cr3BaseAddress, BaseAddress, EFI_PAGES_TO_SIZE(NumPages), Flush); +} + +/** + + This function clears memory encryption bit for the memory region specified by + BaseAddress and Number of pages from the current page table context. + + @param[in] Cr3BaseAddress Cr3 Base Address (if zero then use current CR3) + @param[in] BaseAddress The physical address that is the start address + of a memory region. + @param[in] NumberOfPages The number of pages from start memory region. + @param[in] Flush Flush the caches before clearing the bit + (mostly TRUE except MMIO addresses) + + @retval RETURN_SUCCESS The attributes were cleared for the memory + region. + @retval RETURN_INVALID_PARAMETER Number of pages is zero. + @retval RETURN_UNSUPPORTED Clearing the memory encryption attribute is + not supported + **/ +RETURN_STATUS +EFIAPI +MemEncryptSevSetPageEncMask ( + IN PHYSICAL_ADDRESS Cr3BaseAddress, + IN PHYSICAL_ADDRESS BaseAddress, + IN UINTN NumPages, + IN BOOLEAN Flush + ) +{ + return InternalMemEncryptSevSetMemoryEncrypted (Cr3BaseAddress, BaseAddress, EFI_PAGES_TO_SIZE(NumPages), Flush); +} diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/VirtualMemory.c b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/VirtualMemory.c new file mode 100644 index 000000000000..7cbbf915f443 --- /dev/null +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/VirtualMemory.c @@ -0,0 +1,439 @@ +/** @file + + Virtual Memory Management Services to set or clear the memory encryption bit + +Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.
+Copyright (c) 2017, AMD Incorporated. All rights reserved.
+ +This program and the accompanying materials +are licensed and made available under the terms and conditions of the BSD License +which accompanies this distribution. The full text of the license may be found at +http://opensource.org/licenses/bsd-license.php + +THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. + +Code is derived from MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.c + +**/ + +#include +#include +#include + +#include "VirtualMemory.h" + +STATIC BOOLEAN mAddressEncMaskChecked = FALSE; +STATIC UINT64 mAddressEncMask; + +typedef enum { + SetCBit, + ClearCBit +} MAP_RANGE_MODE; + +/** + Get the memory encryption mask + + @param[out] EncryptionMask contains the pte mask. + +**/ +STATIC +UINT64 +GetMemEncryptionAddressMask ( + VOID + ) +{ + UINT64 EncryptionMask; + CPUID_MEMORY_ENCRYPTION_INFO_EBX Ebx; + + if (mAddressEncMaskChecked) { + return mAddressEncMask; + } + + // + // CPUID Fn8000_001F[EBX] Bit 0:5 (memory encryption bit position) + // + AsmCpuid (CPUID_MEMORY_ENCRYPTION_INFO, NULL, &Ebx.Uint32, NULL, NULL); + EncryptionMask = LShiftU64 (1, Ebx.Bits.PtePosBits); + + mAddressEncMask = EncryptionMask & PAGING_1G_ADDRESS_MASK_64; + mAddressEncMaskChecked = TRUE; + + return mAddressEncMask; +} + +/** + Split 2M page to 4K. + + @param[in] PhysicalAddress Start physical address the 2M page covered. + @param[in, out] PageEntry2M Pointer to 2M page entry. + @param[in] StackBase Stack base address. + @param[in] StackSize Stack size. + +**/ +STATIC +VOID +Split2MPageTo4K ( + IN PHYSICAL_ADDRESS PhysicalAddress, + IN OUT UINT64 *PageEntry2M, + IN PHYSICAL_ADDRESS StackBase, + IN UINTN StackSize + ) +{ + PHYSICAL_ADDRESS PhysicalAddress4K; + UINTN IndexOfPageTableEntries; + PAGE_TABLE_4K_ENTRY *PageTableEntry, *PageTableEntry1; + UINT64 AddressEncMask; + + PageTableEntry = AllocatePages(1); + + PageTableEntry1 = PageTableEntry; + + AddressEncMask = GetMemEncryptionAddressMask (); + + ASSERT (PageTableEntry != NULL); + ASSERT (*PageEntry2M & AddressEncMask); + + PhysicalAddress4K = PhysicalAddress; + for (IndexOfPageTableEntries = 0; IndexOfPageTableEntries < 512; IndexOfPageTableEntries++, PageTableEntry++, PhysicalAddress4K += SIZE_4KB) { + // + // Fill in the Page Table entries + // + PageTableEntry->Uint64 = (UINT64) PhysicalAddress4K | AddressEncMask; + PageTableEntry->Bits.ReadWrite = 1; + PageTableEntry->Bits.Present = 1; + if ((PhysicalAddress4K >= StackBase) && (PhysicalAddress4K < StackBase + StackSize)) { + // + // Set Nx bit for stack. + // + PageTableEntry->Bits.Nx = 1; + } + } + + // + // Fill in 2M page entry. + // + *PageEntry2M = (UINT64) (UINTN) PageTableEntry1 | IA32_PG_P | IA32_PG_RW | AddressEncMask; +} + +/** + Split 1G page to 2M. + + @param[in] PhysicalAddress Start physical address the 1G page covered. + @param[in, out] PageEntry1G Pointer to 1G page entry. + @param[in] StackBase Stack base address. + @param[in] StackSize Stack size. + +**/ +STATIC +VOID +Split1GPageTo2M ( + IN PHYSICAL_ADDRESS PhysicalAddress, + IN OUT UINT64 *PageEntry1G, + IN PHYSICAL_ADDRESS StackBase, + IN UINTN StackSize + ) +{ + PHYSICAL_ADDRESS PhysicalAddress2M; + UINTN IndexOfPageDirectoryEntries; + PAGE_TABLE_ENTRY *PageDirectoryEntry; + UINT64 AddressEncMask; + + PageDirectoryEntry = AllocatePages(1); + + AddressEncMask = GetMemEncryptionAddressMask (); + ASSERT (PageDirectoryEntry != NULL); + ASSERT (*PageEntry1G & GetMemEncryptionAddressMask ()); + // + // Fill in 1G page entry. + // + *PageEntry1G = (UINT64) (UINTN) PageDirectoryEntry | IA32_PG_P | IA32_PG_RW | AddressEncMask; + + PhysicalAddress2M = PhysicalAddress; + for (IndexOfPageDirectoryEntries = 0; IndexOfPageDirectoryEntries < 512; IndexOfPageDirectoryEntries++, PageDirectoryEntry++, PhysicalAddress2M += SIZE_2MB) { + if ((PhysicalAddress2M < StackBase + StackSize) && ((PhysicalAddress2M + SIZE_2MB) > StackBase)) { + // + // Need to split this 2M page that covers stack range. + // + Split2MPageTo4K (PhysicalAddress2M, (UINT64 *) PageDirectoryEntry, StackBase, StackSize); + } else { + // + // Fill in the Page Directory entries + // + PageDirectoryEntry->Uint64 = (UINT64) PhysicalAddress2M | AddressEncMask; + PageDirectoryEntry->Bits.ReadWrite = 1; + PageDirectoryEntry->Bits.Present = 1; + PageDirectoryEntry->Bits.MustBe1 = 1; + } + } +} + + +/** + Set or Clear the memory encryption bit + + @param[in] PagetablePoint Page table entry pointer (PTE). + @param[in] Mode Set or Clear encryption bit + +**/ +STATIC VOID +SetOrClearCBit( + IN OUT UINT64* PageTablePointer, + IN MAP_RANGE_MODE Mode + ) +{ + UINT64 AddressEncMask; + + AddressEncMask = GetMemEncryptionAddressMask (); + + if (Mode == SetCBit) { + *PageTablePointer |= AddressEncMask; + } else { + *PageTablePointer &= ~AddressEncMask; + } + +} + +/** + This function either sets or clears memory encryption bit for the memory region + specified by PhysicalAddress and length from the current page table context. + + The function iterates through the physicalAddress one page at a time, and set + or clears the memory encryption mask in the page table. If it encounters + that a given physical address range is part of large page then it attempts to + change the attribute at one go (based on size), otherwise it splits the + large pages into smaller (e.g 2M page into 4K pages) and then try to set or + clear the encryption bit on the smallest page size. + + @param[in] PhysicalAddress The physical address that is the start + address of a memory region. + @param[in] Length The length of memory region + @param[in] Mode Set or Clear mode + @param[in] Flush Flush the caches before applying the + encryption mask + + @retval RETURN_SUCCESS The attributes were cleared for the memory + region. + @retval RETURN_INVALID_PARAMETER Number of pages is zero. + @retval RETURN_UNSUPPORTED Setting the memory encyrption attribute is + not supported +**/ + +STATIC +RETURN_STATUS +EFIAPI +SetMemoryEncDec ( + IN PHYSICAL_ADDRESS Cr3BaseAddress, + IN PHYSICAL_ADDRESS PhysicalAddress, + IN UINTN Length, + IN MAP_RANGE_MODE Mode, + IN BOOLEAN CacheFlush + ) +{ + PAGE_MAP_AND_DIRECTORY_POINTER *PageMapLevel4Entry; + PAGE_MAP_AND_DIRECTORY_POINTER *PageUpperDirectoryPointerEntry; + PAGE_MAP_AND_DIRECTORY_POINTER *PageDirectoryPointerEntry; + PAGE_TABLE_1G_ENTRY *PageDirectory1GEntry; + PAGE_TABLE_ENTRY *PageDirectory2MEntry; + PAGE_TABLE_4K_ENTRY *PageTableEntry; + UINT64 PgTableMask; + UINT64 AddressEncMask; + + // + // Check if we have a valid memory encryption mask + // + AddressEncMask = GetMemEncryptionAddressMask (); + if (!AddressEncMask) { + return RETURN_ACCESS_DENIED; + } + + PgTableMask = AddressEncMask | EFI_PAGE_MASK; + + if (Length == 0) { + return RETURN_INVALID_PARAMETER; + } + + // + // We are going to change the memory encryption attribute from C=0 -> C=1 or + // vice versa Flush the caches to ensure that data is written into memory with + // correct C-bit + // + if (CacheFlush) { + WriteBackInvalidateDataCacheRange((VOID*) (UINTN)PhysicalAddress, Length); + } + + while (Length) + { + // + // If Cr3BaseAddress is not specified then read the current CR3 + // + if (Cr3BaseAddress == 0) { + Cr3BaseAddress = AsmReadCr3(); + } + + PageMapLevel4Entry = (VOID*) (Cr3BaseAddress & ~PgTableMask); + PageMapLevel4Entry += PML4_OFFSET(PhysicalAddress); + if (!PageMapLevel4Entry->Bits.Present) { + DEBUG ((DEBUG_WARN, + "%a:%a ERROR bad PML4 for %lx\n", gEfiCallerBaseName, __FUNCTION__, + PhysicalAddress)); + return RETURN_NO_MAPPING; + } + + PageDirectory1GEntry = (VOID*) ((PageMapLevel4Entry->Bits.PageTableBaseAddress<<12) & ~PgTableMask); + PageDirectory1GEntry += PDP_OFFSET(PhysicalAddress); + if (!PageDirectory1GEntry->Bits.Present) { + DEBUG ((DEBUG_WARN, + "%a:%a ERROR bad PDPE for %lx\n", gEfiCallerBaseName, + __FUNCTION__, PhysicalAddress)); + return RETURN_NO_MAPPING; + } + + // + // If the MustBe1 bit is not 1, it's not actually a 1GB entry + // + if (PageDirectory1GEntry->Bits.MustBe1) { + // + // Valid 1GB page + // If we have at least 1GB to go, we can just update this entry + // + if (!(PhysicalAddress & (BIT30 - 1)) && Length >= BIT30) { + SetOrClearCBit(&PageDirectory1GEntry->Uint64, Mode); + DEBUG ((DEBUG_VERBOSE, + "%a:%a Updated 1GB entry for %lx\n", gEfiCallerBaseName, + __FUNCTION__, PhysicalAddress)); + PhysicalAddress += BIT30; + Length -= BIT30; + } else { + // + // We must split the page + // + DEBUG ((DEBUG_VERBOSE, + "%a:%a Spliting 1GB page\n", gEfiCallerBaseName, __FUNCTION__)); + Split1GPageTo2M(((UINT64)PageDirectory1GEntry->Bits.PageTableBaseAddress)<<30, (UINT64*) PageDirectory1GEntry, 0, 0); + continue; + } + } else { + // + // Actually a PDP + // + PageUpperDirectoryPointerEntry = (PAGE_MAP_AND_DIRECTORY_POINTER*) PageDirectory1GEntry; + PageDirectory2MEntry = (VOID*) ((PageUpperDirectoryPointerEntry->Bits.PageTableBaseAddress<<12) & ~PgTableMask); + PageDirectory2MEntry += PDE_OFFSET(PhysicalAddress); + if (!PageDirectory2MEntry->Bits.Present) { + DEBUG ((DEBUG_WARN, + "%a:%a ERROR bad PDE for %lx\n", gEfiCallerBaseName, __FUNCTION__, + PhysicalAddress)); + return RETURN_NO_MAPPING; + } + // + // If the MustBe1 bit is not a 1, it's not a 2MB entry + // + if (PageDirectory2MEntry->Bits.MustBe1) { + // + // Valid 2MB page + // If we have at least 2MB left to go, we can just update this entry + // + if (!(PhysicalAddress & (BIT21-1)) && Length >= BIT21) { + SetOrClearCBit (&PageDirectory2MEntry->Uint64, Mode); + PhysicalAddress += BIT21; + Length -= BIT21; + } else { + // + // We must split up this page into 4K pages + // + DEBUG ((DEBUG_VERBOSE, + "%a:%a Spliting 2MB page at %lx\n", gEfiCallerBaseName,__FUNCTION__, + PhysicalAddress)); + Split2MPageTo4K (((UINT64)PageDirectory2MEntry->Bits.PageTableBaseAddress) << 21, (UINT64*) PageDirectory2MEntry, 0, 0); + continue; + } + } else { + PageDirectoryPointerEntry = (PAGE_MAP_AND_DIRECTORY_POINTER*) PageDirectory2MEntry; + PageTableEntry = (VOID*) (PageDirectoryPointerEntry->Bits.PageTableBaseAddress<<12 & ~PgTableMask); + PageTableEntry += PTE_OFFSET(PhysicalAddress); + if (!PageTableEntry->Bits.Present) { + DEBUG ((DEBUG_WARN, + "%a:%a ERROR bad PTE for %lx\n", gEfiCallerBaseName, + __FUNCTION__, PhysicalAddress)); + return RETURN_NO_MAPPING; + } + SetOrClearCBit (&PageTableEntry->Uint64, Mode); + PhysicalAddress += EFI_PAGE_SIZE; + Length -= EFI_PAGE_SIZE; + } + } + } + + // + // Flush TLB + // + CpuFlushTlb(); + + return RETURN_SUCCESS; +} + +/** + This function clears memory encryption bit for the memory region specified by + PhysicalAddress and length from the current page table context. + + @param[in] PhysicalAddress The physical address that is the start + address of a memory region. + @param[in] Length The length of memory region + @param[in] Flush Flush the caches before applying the + encryption mask + + @retval RETURN_SUCCESS The attributes were cleared for the memory + region. + @retval RETURN_INVALID_PARAMETER Number of pages is zero. + @retval RETURN_UNSUPPORTED Setting the memory encyrption attribute is + not supported +**/ +RETURN_STATUS +EFIAPI +InternalMemEncryptSevSetMemoryDecrypted ( + IN PHYSICAL_ADDRESS Cr3BaseAddress, + IN PHYSICAL_ADDRESS PhysicalAddress, + IN UINTN Length, + IN BOOLEAN Flush + ) +{ + + DEBUG ((DEBUG_VERBOSE, + "%a:%a Clear C-bit Cr3 %Lx Base %Lx Length %Lx flush %d\n", + gEfiCallerBaseName, __FUNCTION__, Cr3BaseAddress, PhysicalAddress, Length, + Flush)); + return SetMemoryEncDec (Cr3BaseAddress, PhysicalAddress, Length, ClearCBit, Flush); +} + +/** + This function sets memory encryption bit for the memory region specified by + PhysicalAddress and length from the current page table context. + + @param[in] PhysicalAddress The physical address that is the start address + of a memory region. + @param[in] Length The length of memory region + @param[in] Flush Flush the caches before applying the + encryption mask + + @retval RETURN_SUCCESS The attributes were cleared for the memory + region. + @retval RETURN_INVALID_PARAMETER Number of pages is zero. + @retval RETURN_UNSUPPORTED Setting the memory encyrption attribute is + not supported +**/ +RETURN_STATUS +EFIAPI +InternalMemEncryptSevSetMemoryEncrypted ( + IN PHYSICAL_ADDRESS Cr3BaseAddress, + IN PHYSICAL_ADDRESS PhysicalAddress, + IN UINTN Length, + IN BOOLEAN Flush + ) +{ + DEBUG ((DEBUG_VERBOSE, + "%a:%a Set C-bit Cr3 %Lx Base %Lx Length %Lx flush %d\n", + gEfiCallerBaseName, __FUNCTION__, Cr3BaseAddress, PhysicalAddress, Length, + Flush)); + return SetMemoryEncDec (Cr3BaseAddress, PhysicalAddress, Length, SetCBit, Flush); +} -- 2.7.4