(a) Unlawful Restraint. Any person who is imprisoned or whose liberty is restrained pursuant to a criminal conviction may at any time, as of right, file a written motion requesting the trial judge to release him or her or to correct the sentence then being served upon the ground that the confinement or restraint was imposed in violation of the Constitution or laws of the United States or of the Commonwealth of Massachusetts.

(b) New Trial. The trial judge upon motion in writing may grant a new trial at any time if it appears that justice may not have been done. Upon the motion the trial judge shall make such findings of fact as are necessary to resolve the defendant's allegations of error of law.

(c) Post Conviction Procedure.

(1) Service and Notice. The moving party shall serve the office of the prosecutor who represented the Commonwealth in the trial court with a copy of any motion filed under this rule.

(2) Waiver. All grounds for relief claimed by a defendant under subdivisions (a) and (b) of this rule shall be raised by the defendant in the original or amended motion. Any grounds not so raised are waived unless the judge in the exercise of discretion permits them to be raised in a subsequent motion, or unless such grounds could not reasonably have been raised in the original or amended motion.

(3) Affidavits. Moving parties shall file and serve and parties opposing a motion may file and serve affidavits where appropriate in support of their respective positions. The judge may on rule on the issue or issues presented by such motion on the basis of the facts alleged in the affidavits without further hearing if no substantial issue is raised by the motion or affidavits.

(4) Discovery. Where affidavits filed by the moving party under subdivision (c)(3) establish a prima facie case for relief, the judge on motion of any party, after notice to the opposing party and an opportunity to be heard, may authorize such discovery as is deemed appropriate, subject to appropriate protective order.

(5) Counsel. The judge in the exercise of discretion may assign or appoint counsel in accordance with the provisions of these rules to represent a defendant in the preparation and presentation of motions filed under subdivisions (a) and (b) of this rule. The court, after notice to the Commonwealth and an opportunity to be heard, may also exercise discretion to allow the defendant costs associated with the preparation and presentation of a motion under this rule.

(6) Presence of Moving Party. A judge may entertain and determine a motion under subdivisions (a) and (b) of this rule without requiring the presence of the moving party at the hearing.

(7) Place and Time of Hearing. All motions under subdivisions (a) and (b) of this rule may be heard by the trial judge wherever the judge is then sitting. The parties shall have at least 30 days notice of any hearing unless the judge determines that good cause exists to order the hearing held sooner.

(8) Appeal. An appeal from a final order under this rule may be taken to the Appeals Court, or to the Supreme Judicial Court in an appropriate case, by either party.

(A) If an appeal is taken, the defendant shall not be discharged from custody pending final decision upon the appeal; provided, however, that the defendant may, in the discretion of the judge, be admitted to bail pending decision of the appeal.

(B) If an appeal or application therefor is taken by the Commonwealth, upon written motion supported by affidavit, the Appeals Court or the Supreme Judicial Court may determine and approve payment to the defendant of the costs of appeal together with reasonable attorney's fees, if any, to be paid on the order of the trial court after entry of the rescript or the denial of the application. If the final order grants relief other than a discharge from custody, the trial court or the court in which the appeal is pending may, upon application by the Commonwealth, in its discretion, and upon such conditions as it deems just, stay the execution of the order pending final determination of the matter.

(9) Appeal Under G. L. c. 278, § 33E . If an appeal or application for leave to appeal is taken by the Commonwealth under the provisions of Chapter 278, Section 33E , upon written notice supported by affidavit, the Supreme Judicial Court may determine and approve payment to the defendant of the costs of appeal together with reasonable attorney's fees to be paid on order of the trial court after entry of the rescript or the denial of the application.